# a 2Darray of char pointers...?..?

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok.. all i want is a 2d array of char pointers.. char * m_pTextboxDara[2][]; that will not work... what have i done wrong.

##### Share on other sites
You dont need to put the asterisk- just put the two brackets:

char myChar[2][] = { "awaskdg", "kasdgh" }

And now both myChar[0] and myChar[1] are pointers (basically).

But I would really, really, really, suggest you use the string library.

##### Share on other sites
The problem is that the second dimension of your array is undefined. You must define it when you define a variable. You can leave it undefined only when declaring an argument (and only for the last dimension of an array).

Here's an alternative, if you need jagged arrays:
char ***my_array;my_array = new char**[2];my_array[0] = new char*[10];my_array[1] = new char*[11]; // jagged array

##### Share on other sites
How you do it all depends on whether you know the size of the array you want in advance or not. Suppose you know that what you want is a 10 x 10 array of char pointers, then you do something like this:

char* m_pTextboxData[10][10];

And then you of course have to remember to initialize the entries, because they will be something other than NULL pointers probably and so you might have some problems if you try to use the entries without proper initialization.

On the other hand, if you do *not* know in advance what size you want your matrix of char pointers to be, then you have to basically dynamically allocate the array. Your declaration would then look like this:

char*** m_pTextboxData;

One star here is because you want char* to be stored in the array, and then another for each of the two dimensions you want your array to have. Then you have to allocate the first dimension of the array:

m_pTextboxData = malloc(width * sizeof(char**));

Where width is the width of your array. Then, you have to allocate the second dimension of the array:

for (int i = 0; i < width; i++) {
m_pTextboxData = malloc(height * sizeof(char*));
}

Finally, you can fill your array with your char pointers. Of course, if you do it this way, you must remember to free the stuff you allocated when you are done using it.

Hope that helps.

Vovan

EDIT: Ugh, I suppose I type too slow. :) I also wasn't sure if we were talking C or C++ here?

##### Share on other sites
thanks guys ...... got it to work perfect !!!!

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011470
• ### Who's Online (See full list)

There are no registered users currently online

×