View Single Post
Old 02-07-07, 04:40 PM   #9
Subtestube's Avatar
Join Date: Aug 2004
Location: Wellington, New Zealand
Posts: 1,365
Default Re: String Array in C

I notice someone mentioned "are you really using vanilla C rather than C++?" and I'm going to have to ask this as well. If so, is there a particular reason? Strings in vanilla C can get quite hairy, because they are just arrays of characters. If you declare something to be too small, you often won't be stopped from just overwriting memory that belongs to something else.

If you can use C++, I'd strongly recommend you do, because it'll only be fractionally slower, you can still use a straight procedural style of coding, and you get access to possibly the most useful thing ever: The Standard Template Library. Using STL you can do two things that will make your program more tidy. For one, you can use a vector to store your strings rather than an array, which will resize itself tidily and won't require you to know in advance how big it will be. (Yes, you could actually manually allocate memory using malloc, and then reallocate when you overflowed the memory you'd set aside, but that's painfully manual, and will open you up to all sorts of other possible problems if you're slightly careless.) Secondly, you can actually use standard strings, which allow for assignment more nicely than straight char*s, and, again, will resize nicely. Even if you don't like standard strings (which I'd understand, as of all the STL containers they're... not the most complete - they'd benefit hugely from an sprintf() function), you could still fill a vector with char*s.

I do have one recommendation if you do go down the STL road though - fill the Vector with string pointers rather than strings. Now it's true that if you do that, you'll need to 'new' each string, and 'free' it when you destroy the vector, but strings, being STL containers will resize themselves quite a lot, and so will the vector itself. As you're probably working on a PC, where you have effectively infinite memory and fragmentation isn't such a concern, this probably doesn't matter so much, but it's good practice to try to avoid things constantly reallocating.

Edit: Also, I believe if you did want to do it using a straight array of pointers to strings, you could just go
char* myarray[10];
myarray[x] = (char*)malloc( sizeof(char) * numberOfCharacters );

strcpy( myarray[x], "Hello!" );

Edit 2: Oops - didn't see that you'd pretty much already written that.

Note: You will want to use strcpy/sprintf and such things for string assignments.
Dr Possible: Core 2 Duo E6400 on Gigabyte GA-965P-DS4. Galaxy GeForce 7600GT. 2GB Corsair XMS 2 DDR2-6400 RAM (CL5). ATi Theatre 550 Pro. Windows XP MCE. All stored in Piano black Antec Sonata II, with a broken door.

Mobile: ASUS M2400N, Pentium M 1.5 GHz. 512 MB DDR RAM. Intel EXTREME graphics. Windows XP SP 2 / Ubuntu 5.10.

Ridiculous DOES not have an 'e' in it. It comes from "ridicule" and has less than nothing to do with the colour red.

Last edited by Subtestube; 02-07-07 at 05:05 PM.
Subtestube is offline   Reply With Quote