Dumb question...
How do I create a pointer that points to arrays 20-units long?
Ex.
"char *data[20]" Creates an array of 20 char*''s
I want to create a char* that points to an array of arrays 20 chars long apiece. That is, without doing this:
char **data;
data = new char[some_len];
for(int i = 0; i != some_len; i++)
{
data = new char[20];
};
==================
My (soon-to-be) Cherished Cookie of Appreciation:
-- MattB - for WinSock advice --
I think it would look like
char (*data)[20] = new char[some_len][20];
[edited by - smart_idiot on December 24, 2002 5:19:26 AM]
char (*data)[20] = new char[some_len][20];
[edited by - smart_idiot on December 24, 2002 5:19:26 AM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
char pGrid1[20][20];
char** pGrid2 = new Char[20][20];
Essentially the above creates a pointer to an array of 20 pointers which point to arrays of 20 chars. Also remember that an array is nothing more than a pointer that points to a region of memory that has been put aside for it. In many circumstances it would be easier to have a single array, like so.
char pGrid3[400];
One advantage of the previous method is the ability to reorder the items.
char* temp= pGrid2[12];
pGrid[12] = pGrid[3];
pGrid[3] = temp;
In that example the 20 chars pointed to by pGrid2[12] are now pointed to by pGrid[3] and vice-versa.
-----------
"If the universe became a place without chaos then doubtless it would do something unexpected."
char** pGrid2 = new Char[20][20];
Essentially the above creates a pointer to an array of 20 pointers which point to arrays of 20 chars. Also remember that an array is nothing more than a pointer that points to a region of memory that has been put aside for it. In many circumstances it would be easier to have a single array, like so.
char pGrid3[400];
One advantage of the previous method is the ability to reorder the items.
char* temp= pGrid2[12];
pGrid[12] = pGrid[3];
pGrid[3] = temp;
In that example the 20 chars pointed to by pGrid2[12] are now pointed to by pGrid[3] and vice-versa.
-----------
"If the universe became a place without chaos then doubtless it would do something unexpected."
-----------"If the universe became a place without chaos then doubtless it would do something unexpected."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement