Strings and lists (C++)

I’m having trouble creating a container of strings for use. For example, I’m trying to store strings in an array structure so that I can reference each string by index. However, upon creating a string array, it’s just like creating a single string of length of the array.

For instance:

char alist[10000]

… would be a character of length up to 10000. If I reference any index in “aList”, it would just return the value of the character at that index.

I’m thinking of using a 2-dimensional array but I don’t know if that’s possible with strings. Am I missing something here? Any help would be appreciated.

Yes, a two-dimensional array is possible with strings. F’rinstance:

char a[256][1024];

would create space for 256 strings, each up to 1024 characters long. If you had strings of radically different length, and space was an issue, you could also do:

char *a[256];

to set up an array of 256 pointers, allocate your strings separately, and set your pointers to the beginning of each individual string. You would be using 1024 bytes to allocate the pointers, but you could use less memory for allocating the actual strings, if you were clever about it. However, for the easiest-to-debug code, I’d recommend the former.

char alist[10000] is an array of 10000 strings, rather than 10000 characters, and alist is the i’th string, an element of type char *. The alist variable can also then be treated as a char **. An array declaration merely allocates storage for a specific type, and creates a variable which can be treated to as a pointer to that type. Conversely, you can use “” notation to index from a pointer, based on it’s type.

It is up to you to work out the storage for each char * you populate the array with. Without knowing your exact problem, I might hazard a guess that there is a better way to do what you want to do. Large declared storage often indicates that somebody is not yet comfortable with dealing with dynamic memory allocation.

One of the most important lessons for a beginning C programmer is to become comfortable with the way it confounds the concept of a pointer with the concept of an array, much to the disgust of many linguistic purists.

Another way to approach this whole thing would be to use the Standard Template Library (STL). It has built in classes for ‘string’ as well as pretty much every kind of list, collection or vector you can imagine.

STL encapsulates in a more C+±like way a lot of the things that C programmers found themselves doing over and over again, e.g. string manipulating, collecitons.

There is a bit of a learning curve but once you’ve mastered the basics, things work pretty much as you’d expect. You get all sorts of stuff like sorting and searching for free, it’s all tested and highly optimized code.

For your particular case, a vector of string might do the job:

std::vector<std::string> myCollection;

Look for “Standard Template Library” in Google.

or better yet, go here:


Strings in C and C++ positively suck; that’s why I prefer Java these days.

Technically, isn’t that an array of 10,000 character pointers (instead of 10,000 strings)? Because for each string you’ll still have to allocate memory and set the alist* entry to point to it. Or am I confused again?

Just to point out the obvious, that C++ won’t do any bounds checking on that array. You can easily copy a 2000-character string into a[0] and have it clobber parts of a[1] in the process.