Creating an array of strings C [pointer type] [c strings]

Q: I try a simple C array of 10 strings and display them. I have cut the code below.

gcc gives me a warning:

warning: passing argument 2 of strcpy from incompatible

at line with strcpy.

As once ran the program, I get a seg fault. The SEG errors around the strcpy () line, not even to display it. So I do not know whats wrong. I need a pointer short somewhere or whatever?


Re: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


Re: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! :(


Re: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.


Re: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.


Re: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.


Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]