Creating & Populating String Arrays in C

  • Thread starter Thread starter jpatterson
  • Start date Start date
  • Tags Tags
    Arrays String
Click For Summary

Discussion Overview

The discussion revolves around creating and populating arrays of strings in the C programming language. Participants explore different methods for managing string arrays, including memory allocation and initialization, while addressing potential pitfalls and runtime errors associated with improper usage.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion about creating an array of strings using the declaration char *array_name[10] and encounters a runtime error when attempting to use gets(array_name[0]).
  • Another participant points out the need to initialize the pointers in the array before use, indicating that the initial declaration does not allocate memory for the strings themselves.
  • A participant clarifies the difference between declaring a character array and a pointer to a character array, emphasizing the importance of proper memory management in C.
  • Suggestions are made for allocating an array of character arrays using char buffer[10][200];, allowing for storage of multiple strings, while noting the implications of stack memory for local variables.
  • One participant describes an alternative approach using heap memory, involving reading lines into a buffer and duplicating them into dynamically allocated strings, with a reminder to free the allocated memory afterward.
  • Another participant mentions that processing input lines one at a time may eliminate the need for storing them in an array, suggesting a more efficient approach for certain applications.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method for creating and populating string arrays, as multiple approaches are discussed, each with its own advantages and considerations. The discussion remains unresolved regarding the best practice for the specific use case presented.

Contextual Notes

Participants highlight limitations related to memory management in C, including the necessity of initializing pointers and the implications of stack versus heap allocation. There are also concerns about the potential for runtime errors when using uninitialized pointers.

Who May Find This Useful

Individuals learning C programming, particularly those interested in string manipulation and memory management, may find this discussion beneficial.

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.
 
Technology 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.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
20
Views
2K
Replies
6
Views
6K