Creating an array of C strings

duragezic

Lifer
Oct 11, 1999
11,234
4
81
I'm trying to create a simple array of 10 C strings and display them. I've attached the code below.

gcc gives me a warning:

warning: passing argument 2 of 'strcpy' from incompatible pointer type

at the line using strcpy.

As soon as this program is ran, I get a seg fault. It seg faults around the strcpy() line, not even getting to displaying it. So I'm not sure where it is wrong. I must be short a pointer somewhere or whatever?
 
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
You have created a string array (strArray) that contains a total of 11 entries. Each of these entries contains an address that you must fill.

You created this string array yet did not set any values within it. When you call strcpy it is trying to copy to an address of 0 which causes a segmentation fault (the program does not have permission to this place in memory).

Here is your string array now:

strArray
[0]: NULL
[1]: NULL
[2]: NULL
...
[10]: NULL

Thus, this is how strcpy is getting called:

x = strcpy(NULL /*destination*/, msg /*source*/);

What you should do is fill the elements of strArray with pointers to valid character arrays so that the address will no longer be NULL.

Edited for clarification.

I don't know about your strcpy call, but I never use the return value of strcpy.

Just this
strcpy(NULL /*destination*/, msg /*source*/);

instead of
x=strcpy(NULL /*destination*/, msg /*source*/);

Also, this: char *msg[41] is a string array, not a character array. It is an array of 41 character pointers. You probably meant char msg[41], which is an array of 41 characters.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Ok. The

char *msg[41] is indeed suppose to be a string array, because that is coming from sample code I did not write but must use. So to hold it, would I need

char ***strArray??

And yeah I wasn't sure about that return value for strcpy, so I'll try it without.

Anyway, when you say I need to fill elements of strArray with pointers, isn't that what strcpy does?

If I simply do

strArray[x] = msg

then every element in strArray is the same as msg changes so it points to the same. I need it to save the whole msg in each element in strArray.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You just allocated storage for a set of pointers. Each of those pointers needs to be set to a buffer somewhere before it can be safely used.

strcpy returns the address of an existing buffer, the buffer that you copied to, but that buffer must already exist. strcpy does not do a malloc or new for you.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Ok, so I would declare as

char **strArray = (char **) malloc(10 * sizeof(char *))

and then use a for loop, j = 0 to 9 to

strArray[j] = (char *) malloc(sizeof(char *))

like how a 2D array/grid (e.g. [x][y]) is initialized? I was thinking since this is a static size array I wouldn't have to, but I'm not too sur enow. Is what I posted above the way to do it?


Also, regarding "msg". What I should've said is "args". So indeed it is suppose to be

char *args[41]

since it holds a parsed Unix command. So args[0] is the name of the command, and any following are arguments have at least a dash and a letter so it needs to be an array of character arrays. What I need to do with them is for each one entered, store into an array. I must be doing something wrong since only the name is getting in there, no arguments do. So do I need char ***strArray/char **StrArray[] perhaps?


Bah, now is the time I wish I did it in C++! A STL Vector and String as well as new/delete would fulfill this part of the assignment much easier than what this thread is about! :(
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126

stringzor [ 3 ] = (char* ) malloc( 1024 * sizeof (char) ) ; // give this element a buffer that can hold 1 1023-character string plus \0 terminator

You were close, each element needs a buffer to hold char not char* though