PDA

View Full Version : String Array in C


Tuork
02-07-07, 12:21 AM
I'm new to C (been Java'ing for the past 3 years) and I need some help regarding strings and string arrays.


Basically, I want to read a bunch of names from a txt file and load them up into an array.

How does one go about creating an array of strings in C?


Mind you, I searched around through google but the answers I found I couldn't really comprehend.

I would greatly appreciate any help... this is part of a project I have to hand in by Thursday. :(

rhink
02-07-07, 12:48 AM
A "string" in C is implemented as a null-terminated array of characters.

So an array of strings in C would be an array of arrays, or an array of pointers to arrays.

---->[a][b][c]
[1]---->[1][2][3][4][/0]
[2]---->[b][d][a][/0]

Something like that.

You're actually doing flat C, not C++?

Imbroglio
02-07-07, 02:02 PM
exactly like rhink stated.

"strings" are already an array terminated by null characters

an array of strings:
0: [a][p][p][l][e][\0]
1: [o][r][a][n][g][e][\0]
.
.
.
etc

so if you wanted to get to the 'n' in orange, the location would be [1][3], and that is printed as a char.

i don't know if this helps, if your question is more advanced i could probably answer it if you ask the question.

are you looking to create an array of strings? like a matrix array of char? looking at your question again, i'm not quite sure what you're asking.

Tuork
02-07-07, 02:44 PM
I think I kinda figured it out. I know it's more or less a 2-dimensional array of chars, I just didn't know how to declare it.

As far as I can tell, its something like this:

char *myArray[10];
char *str = "string!";

myArray[3] = str;

That would leave "string!" in index 3 of myArray, wouldn't it?

Imbroglio
02-07-07, 02:56 PM
I think I kinda figured it out. I know it's more or less a 2-dimensional array of chars, I just didn't know how to declare it.

As far as I can tell, its something like this:

char *myArray[10];
char *str = "string!";

myArray[3] = str;

That would leave "string!" in index 3 of myArray, wouldn't it?


yes, myArray[3] would get the pointer value of str. but don't get confused, if you change what str points to down the line, it would also change the value of myArray[3]

Marvel_us
02-07-07, 03:05 PM
yes, myArray[3] would get the pointer value of str. but don't get confused, if you change what str points to down the line, it would also change the value of myArray[3]

What if he just did the same thing without the pointers....ie:

char myArray[10];
char str = "string!";

myArray[3] = str;

Wouldn't he get the same results or no?

Imbroglio
02-07-07, 03:24 PM
What if he just did the same thing without the pointers....ie:

char myArray[10];
char str = "string!";

myArray[3] = str;

Wouldn't he get the same results or no?

first off, what you wrote is a syntax error :p

second, if he wrote

char myArray[10][X]; //where X is the max size of the string
char str[X] = "string!"; //haven't worked with strings in a while might have to call a function to convert the string to the char array. don't remember. (#include string.h)

then

myArray[3] = str;

then myArray[3] would contain the value of str, ie: "string!" and not a pointer to the pointer of the string.

Marvel_us
02-07-07, 03:43 PM
Whoops wasn't really paying attention.:o

Thanks for answering the question though....hopefully Tuork got it sorted out. I need to go start on some programming homework myself.

Subtestube
02-07-07, 05:40 PM
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.

Tuork
02-08-07, 12:50 AM
Thank you all for the added input.

My deadline was pushed so I guess I'll be working on this over the weekend.

As for the whole C/C++ business, I'll ask my teacher on fruday, but as far as I know, it's supposed to be in C. Yes, C++ would have the advantages you mentioned, but orders are orders :P.

Besides, it's nothing too complex... just a very *VERY* simple initial stage of a P2P program we will build.

Tuork
02-09-07, 11:06 AM
Uuuuupdate:

Well, I figured out how to do the string thingers (I'm using a structure in order to assure scalability).

The real question now is... how to maximize memory efficiency. Right now I am using character arrays of a set size (200) for two variables, name and path. How can I use pointer to manage memory in a more elegant manner?

Imbroglio
02-09-07, 01:02 PM
basically you need to look at testtube's response

you need to memory allocate the size of the string you attempting to store, and be sure to release that memory after you are finished using it.

Bman212121
02-10-07, 02:16 AM
Gotta love homework. :) I need to get working on a program to do inventory. It is going to read in the info from the file into a few vectors (requirement), and allow you to search, and do a few other things with it. I'm writing it in C++, so it won't be as bad as what your doing, but C++ is kind of a pita IMO to do a lot of things I've done in VB .net. I guess I got too used to just creating a form, and programming buttons and textboxes to do their thing. Since this is beginner C++ were doing everything text based. :(

Tuork
02-10-07, 02:34 PM
basically you need to look at testtube's response

you need to memory allocate the size of the string you attempting to store, and be sure to release that memory after you are finished using it.


Aight.

Thanks. :)

Subtestube
02-10-07, 04:21 PM
Aight.

Thanks. :)

If you've got any questions about memory allocation etc, feel free to ask, and I'll try to answer as best I can. I actually work predominantly in C++ these days, and as anyone who's migrated from C to C++ can tell you, avoiding direct malloc/realloc/calloc/etc...ing memory is probably one of the nicest things about the STL. Nevertheless, I've got a pretty good understanding of what's going on ;).

I will be away from my PC for most of today (it's Sunday and I'm currently on leave!), so if you don't get a really prompt reply from me, it's probably because I'm not actually online, rather than that I don't want to help.

ViN86
02-13-07, 06:07 PM
Aight.

Thanks. :)
yea you can get away with not inputting a size in most windows developer programs, but if youre coding from a text file in C, you need to declare the size of the array.

also, make sure you dont accidentally try to access an element outside the array, that will give you a nice crash :D (i speak from experience lol)

i wrote a program that plays a game of battleship and i wrote it in C. in the program, i deal with two dimensional arrays constantly throughout the file. if you want i can send you file. its nicely commented (i was graded on it) so it might help quite a bit. send me a pm if you want it, and ill email it to you ;)

Tuork
02-13-07, 09:50 PM
Thank you all for all your input. you have been more than helpful.

Due to personal reasons (which I will comment on in the following days) I haven't had tiem to work on this. However, all I need to do now is add som fork()'s and I'm basically set. (All part of the assingment)