Creating & Populating String Arrays in C

  • Thread starter Thread starter jpatterson
  • Start date Start date
  • Tags Tags
    Arrays String
AI Thread Summary
To create and populate an array of strings in C, it's essential to understand the difference between declaring character arrays and pointers. The correct way to declare an array of strings is using "char buffer[10][200];" which allocates space for 10 strings, each capable of holding 199 characters plus a null terminator. Using "char* array[10];" only allocates pointers without initializing them, leading to access violations when trying to use functions like gets(). For dynamic memory allocation, one can use a large buffer and then copy strings into allocated memory using strdup, ensuring to free the allocated memory afterward. Proper memory management is crucial in C to avoid runtime errors and memory leaks.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top