SolvedMULTI-DIMESIONAL C-Style Strings

Posts 1–5 of 5 · Page 1 of 1
MULTI-DIMESIONAL C-Style Strings
I tried to use multi-dimensional c-style strings but i keep getting into corners in which i can't get out, so if somebody can explain them for me i would appreciate it.


I have been using it like this:

Code:
char szString[3][40];
also i have read that this is the same:

Code:
char **szString = 0;
I have no idea how to put the strings in it. Thanks in advance.
If you're using the 2nd option you need to dynamically allocate the memory for the char**.

i.e, say you're going to store....20 strings at this stage:

Code:
char **strArray = (char**)calloc(20, sizeof(char*));
So now the top level has been allocated, but there has been no room actually allocated for strings themselves, just the pointers to the strings. To produce a valid pointer you can do it several ways, the easiest being strdup, it will duplicate an existing string into heap memory and give you a pointer to that string, you can then add that pointer to your array:

For example:

Code:
char **strArray = (char**)calloc(20, sizeof(char*));
char *derp = "literal static string";
//now, i don't want to risk the "derp" variable being collected or modified somewhere during runtime, so I'll create my own copy of the string (not the pointer to the string, the string itself.
strArray[0] = strdup(derp);
It might be easier to envision what this is doing when you consider:

Code:
char** == *char[]
So essentially you have an array of pointers-to-string.

As always, pointers can be a mindfuck.
Sorry for late reply. Thanks Jason that makes sense. And ya, as usual pointers are bein annoying, lol.


The only thing annoying me now is that this line is giving me an Assertion error:

Code:
sprintf(szServerRequestArray[i - 2], "somestring %i some string %i some string", WhichOne, i);
Quote Originally Posted by 258456 View Post
Sorry for late reply. Thanks Jason that makes sense. And ya, as usual pointers are bein annoying, lol.


The only thing annoying me now is that this line is giving me an Assertion error:

Code:
sprintf(szServerRequestArray[i - 2], "somestring %i some string %i some string", WhichOne, i);
Im guessing you never allocated any memory for szServerRequestArray[i - 2] to point to. Personally I'd use a buffer and then duplicate.

Code:
char buffer[256];
//other code
sprintf(buffer, "somestring %i some string %i some string", WhichOne, i);
szServerRequestArray[i - 2] = strdup(buffer);
Quote Originally Posted by Jason View Post


Im guessing you never allocated any memory for szServerRequestArray[i - 2] to point to. Personally I'd use a buffer and then duplicate.

Code:
char buffer[256];
//other code
sprintf(buffer, "somestring %i some string %i some string", WhichOne, i);
szServerRequestArray[i - 2] = strdup(buffer);
LOl, this is just a pain, i am just gonna use the function to return the string, the only bad thing is i will need to call the function many more times, but it isn't that bad performance wise. Thanks guys.
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us