This topic is now archived and is closed to further replies.


Dumb question...

Recommended Posts

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 --

Share this post

Link to post
Share on other sites
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."

Share this post

Link to post
Share on other sites
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 ];

Share this post

Link to post
Share on other sites