Jump to content
  • Advertisement
Sign in to follow this  
MTclip

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.

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!