Creating & Populating String Arrays in C

In summary, the conversation discusses the proper way to create an array of strings in C and how to populate one of the elements using the gets() function. It is important to properly initialize each pointer in the array and to use memory management techniques to avoid errors and efficiently store strings. The conversation also mentions different ways of allocating and storing strings, including on the stack or in the heap.
  • #1
jpatterson
3
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.
 
Technology news on Phys.org
  • #2
update - I missed the * in the declaration of array of pointers. You need to initialize the 10 pointers before you use them.
 
Last edited:
  • #3
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.
 
  • #4
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.
 
  • #5
Flip-
Thanks. This was a big help.
 

Related to Creating & Populating String Arrays in C

1. How do I create a string array in C?

To create a string array in C, you can declare an array of characters and assign individual strings to each element. For example, you can declare an array with 5 elements and assign strings to each element using the index number, like array[0] = "string1", array[1] = "string2", etc.

2. Can I initialize a string array in C?

Yes, you can initialize a string array in C by assigning values to each element when declaring the array. For example, you can declare an array with 3 elements and assign strings to each element, like char array[3] = {"string1", "string2", "string3"}.

3. How do I access and modify elements in a string array in C?

To access and modify elements in a string array in C, you can use the index number of the element. For example, to access the first element in the array, you can use array[0], and to modify it, you can assign a new string to it using the assignment operator (=).

4. Can I use loops to populate a string array in C?

Yes, you can use loops to populate a string array in C. You can use a for loop to iterate through the array and assign strings to each element based on the loop counter.

5. How do I print the contents of a string array in C?

To print the contents of a string array in C, you can use a loop to iterate through the array and use the printf() function to print each element. Alternatively, you can use the %s format specifier in the printf() function to print the entire array at once.

Similar threads

  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
Back
Top