Creating & Populating String Arrays in C

  • Thread starter Thread starter jpatterson
  • Start date Start date
  • Tags Tags
    Arrays String
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
jpatterson
Messages
3
Reaction score
0
I'm new to C, so this is probably a dumb question, but I'd appreciate help. If I create (or think I create, anyway) an array of strings with:

char *array_name[10]

and then try to populate an element of the array with:

gets(array_name[0])

everything compiles ok, but I get a runtime error complaining about an access violation.

What is the proper way to create an array of strings and populate one of the elements of the array using gets?

Thanks in advance.
 
Physics news on Phys.org
update - I missed the * in the declaration of array of pointers. You need to initialize the 10 pointers before you use them.
 
Last edited:
I'm sorry, I wasn't being clear. What I'm trying to do is create an array of strings. I.e. string1 string2, string3. I was guessing at *array_name[10], but apparently that just creates one string (with room for 10 characters). How do I create the array of strings.
 
Notice the difference between the declaration "char buffer[80];" and "char* str;". The first allocates an array of 80 characters and let buffer point to the first of these characters, while the later declaration only declares a pointer to a character array. In your declaration "char* array[10];" you allocate an array of 10 character pointers, but you do not initialize each pointer to any sensible value (like an allocated array), hence "gets(array[0])" will pass a null or garbage pointer to gets which will then try to store characters in a place in memory it shouldn't.

In C, memory management is mostly up to the programmer and there are quite many ways of doing this and unfortunately even more ways to mess up.

To allocate an array of character arrays, you can write like "char buffer[10][200];" where you'd get 10 strings up to 199 characters (plus a terminating null character). You can pass each buffer to gets() which will then store each text line in each string buffer (with strange results if a line is more than 199 characters long). Note that even if each line is only a few characters long, each takes up 200 characters of allocated storage. Also, if you declare the buffer inside a function (as an auto-variable), the stack memory allocated to hold the strings will be reclaimed for other things when the function returns, meaning you can only "use" the strings inside the function.

Another slightly more complicated way is to use the heap, where you store each line into a big buffer and then copies only the needed characters into a string allocated of the heap. This could go something like

Code:
char buffer[1000]; /* reserve plenty of room for each line */
char* string[10];
int lines = 0;
while (lines < 10 && gets(buffer)) {
  string[lines++] = strdup(buffer);
}
/* use the string[0] to string[lines-1] for something */
...
/* deallocate strings when done */
for (int i = 0; i < lines; i++) {
  free(string[i]);
}

Depending on your problem it may be possible, or even better, to it in a third way. If for instance you only need to process each input line one at a time (which is a very common thing in text processing) you simply read each line in a loop without any need to store them.
 
Flip-
Thanks. This was a big help.