-1

I got array like this:

char *family[3] ={"son", "father", "sister"};

and I want to align length of each element to the same size by using function strcat(son," "); but I get core dumped instead.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

3

You cannot strcat() to a literal. And the latter is what the three "strings" are.

Literals are placed in read-only memory. They may not be overwritten, trying to do so causes a run-time error.

alk
  • 69,737
  • 10
  • 105
  • 255
0

I think you were trying to write something like this:

char family[3][MAX_LEN] = {"son", "father", "sister"};
strcat (family[i], "   ");

the differences on the definition of "family" with the original:

 char *family[3] ={"son", "father", "sister"};

is that here you have 3 pointers to 3 literal const strings which are read only. Thus you can not modify them with strcat.

Whereas on char s[3][L]={"a","b","c"} you allocate 3 char arrays of lenght L each in the stack and initialize them to 3 literals. So now you may modify them with strcat if L is big enough...

Also, you were using strcat() in a wrong way as @alk pointed out...

rmp
  • 1,053
  • 7
  • 15
0

strcat() appends a copy of a source string to the end of a target string, including the terminating ASCII NUL byte. The initial character of the string to append overwrites the NUL byte at the end of the target string.

Therefore, you cannot set "son" as the target string because it is a literal string,

`strcat(family[0], " "); is what you are trying to do, but that will not work either because family[0] is set to "son\0\0\0\0" already and will cause buffer overflow.

Do this instead:

char *family[3] ={"son   ", "father", "sister"};//now family[0] is initialized with spaces to match length
                                                //(and strcat() statment is not needed)
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • "*now that family[0] has room*" is a misleading argument, as there is also "*room*" in the `char*[]` example you give anyway. – alk Dec 02 '13 at 16:01
  • True, but in the first correct example, strcat does not need to be performed. The intent is to show why it will work, and why it will not work. I will clarify with a comment. Thank you. – ryyker Dec 02 '13 at 16:04
  • 1
    `char * family[3] = {"son\0\0\0\0",...` has "room" and the need to be padded with blanks and `strcat()` will fail. The issue is not about to few or enough memory ("room"). – alk Dec 02 '13 at 16:05
  • I am working on a line to address the rest of you comment, patience please. – ryyker Dec 02 '13 at 16:12
  • I'm sorry but the same argument is here: "*but that will not work either because family[0] only has 4 bytes and needs 7.*" althought this is true, that is **not** the reason for the segmentation violation. – alk Dec 02 '13 at 16:16
  • @alk - Okay - and I saw your point, and was doing a little experiment to convince myself of another approach. In the end, I will just leave the simple initialization example :) Thanks (and for future, I am not opposed to a down-vote when they point out my mistakes) – ryyker Dec 02 '13 at 16:19
  • @alk Changed it. (missed taking that out on last edit. A little rusty after the long weekend :) and +1 for your answer (short, correct) – ryyker Dec 02 '13 at 16:21
  • Please see my updated answer for why the OP's code crashes. – alk Dec 02 '13 at 16:30