1

I am trying to learn C with The C programming Language by K&R. I am trying to write a the strcat() program using pointers.

char *strcat (char *s, char *t){
    char *d;
    d = s;
    while(*s++);
    s--;
    while(*s++ = *t++);
    return d;
}

int main () {
    char *name1;
    char *name2;
    name1 = "stack" ;
    name2 = "overflow";
    printf("%s %s\n", name1, name2);
    printf("strcat out : %s", strcat(name1, name2));
    return 0;
}

But I am getting the ouput as

stack overflow
Segmentation fault

Why is it not working ? Can anybody clarify the mistake here..

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
noufal
  • 940
  • 3
  • 15
  • 32
  • 1
    Is this C, or C++? Why do you have both tags? – tckmn Aug 31 '13 at 15:52
  • 1
    @Doorknob - the answer is the same for both languages. – Pete Becker Aug 31 '13 at 15:58
  • You need to learn arrays and pointer, declare your `char name1[SIZE];` and `char name2[SIZE];` and replace assignment statements `name1 = "stack";` as `strcpy(name1, "stack");`, `strcpy(name2, "overflow");` – Grijesh Chauhan Aug 31 '13 at 16:10
  • Read: [Difference between `char *str` and `char str[]` and how both stores in memory?](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) – Grijesh Chauhan Aug 31 '13 at 16:11
  • possible duplicate of [Implementing strcat using pointers](http://stackoverflow.com/questions/18398180/implementing-strcat-using-pointers) – Grijesh Chauhan Aug 31 '13 at 16:12
  • @PeteBecker: I think for C++ the answer would be "Use `std::string` instead of `char*`, for goodness sake!" – Fred Larson Aug 31 '13 at 22:55
  • @FredLarson - the answer to "why does my code crash when I write to a literal string" is not "use `std::string`. That adds overhead without getting at the root of the problem. – Pete Becker Sep 01 '13 at 13:11

4 Answers4

2

Because the pointers are pointers to literals, and not only are those read only the destination is not big enough to contain both the strings. For at least string1 you would want to use an array of at least big enough size to contain both strings (including the terminator). Like:

char string1[128] = "Stack";

or

char string1[128];
strcpy(string1, "Stack");
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

In this line:

while(*s++ = *t++);

you dereference s which points to one-past-the-end of the char array "stack". This causes segmentation fault error.

cpp
  • 3,743
  • 3
  • 24
  • 38
0

The program tries to append characters to the literal string "stack". Don't do that; create an array of characters that's large enough to hold the result, copy the first string into the array, and then strcat the rest of the string.

Oh, and name your function something other than strcat. That name is already taken.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0
 char *name1;
 char *name2;
 name1 = "stack" ;
 name2 = "overflow";

==>

 char name1[20];
 char name2[10];
 memcpy(name1,"stack") ;
 memcpy(name2,"overflow");
Lidong Guo
  • 2,817
  • 2
  • 19
  • 31