Advertisement

Dumb question...

Started by December 24, 2002 02:27 AM
2 comments, last by zackriggle 21 years, 10 months ago
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]
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.
Advertisement
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."
-----------"If the universe became a place without chaos then doubtless it would do something unexpected."
that one might give a compiler error.. not sure... but a good thing is to define a new type...

  typedef char pChar20_t[20];pChar20_t *array;array = new pChar20_t [ some_length ];  
____________________________________MSN | AIM | YIM | ICQ

This topic is closed to new replies.

Advertisement