Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C string questions

This thread is locked; no one can reply to it. rss feed Print
C string questions
KeithS
Member #11,950
May 2010

* * * * * * * * * * *

My Noobish Blog

relpatseht
Member #5,034
September 2004
avatar

What you have there is an array of string pointers. They're static strings. If that's what you want, that's what you have.

char *string_database[4] = {'\0'};// creates an array of 4 string pointers, all initialized to NULL
char *string_database[4] = {}; // equivalent
char *string_database[4] = { NULL, NULL, NULL, NULL }; // equivalent
char *string_database[] = { NULL, NULL, NULL, NULL }; // equivalent

The following lines set the individual array elements to strings stored in the TEXT section of your binary. These aren't dynamic strings. There's a chance there's only 1 copy of each for the program (I.E., if you used the string "Florida" in many places, then the program changed one of them, it would be changed in all places), depending on compiler flags. They really should be declared const char *, since they are stored in the binary.

If it were me, I'd do this:

// const char * const makes string_database a collection of pointers that can't
// be changed to characters that also can't be changed.
const char * const string_database[] = { "Florida", "Oregon", "California", "Georgia" };

C/C++ doesn't have dynamic multidimensional arrays as I would define dynamic. The best you can do is have an array of pointers, and allocate each to a difference size array:

char * string_database[4];
string_database[0] = (char*)malloc(sizeof(char)*10); // can now hold 10 characters in [0]
string_database[1] = (char*)malloc(sizeof(char)*16); // can hold 16 characters in string[1]

The important distinction between both of these methods, and a multidimensional array (char *string_database[4][15];) is the memory layout. In the multidimensional array, string_database[0][15] == string_database[1][0]. In the array of pointers example, string_database[0][strlen("Florida")+1] is undefined.

bamccaig
Member #7,536
July 2006
avatar

You probably should clarify what you mean by "dynamic" and "multidimension". Technically string_database is not dynamic NOR multidimensional. It is a single-dimensional, fixed-size array of pointers to strings. If you really wanted a 2D array of strings you might do something like this:

char string_database[4][255] = {0};
strcpy(string_database[0], "Florida");
strcpy(string_database[1], "Oregon");
strcpy(string_database[2], "California");
strcpy(string_database[3], "Georgia");

Note however that these strings are still not really "dynamic" in the usual sense. Usually that means they are allocated on the heap at run-time, and possibly their lengths can vary instead of having a fixed length. Instead, I allocated 255 characters for each string regardless of whether they need them, and we cannot store a string longer than 254 characters (plus a NUL byte).

What you should do depends on what you're trying to do. There's no universally correct way to do this. There are better and worse ways for specific problems. If you describe what you're doing with this we can help to steer you towards an ideal solution.

KeithS
Member #11,950
May 2010

Sorry, I posted twice...

There is more, here: https://www.allegro.cc/forums/thread/617409

TY for the replies! And relpatseht, yes, that is one other thing I wanted to know; if you could, indeed, have an array of disimilar length strings (as in; the memory reserved for them individually).

Thanks again!

* * * * * * * * * * *

My Noobish Blog

Go to: