Saturday, 31 August 2013

Error while writing strcat() using pointers

Error while writing strcat() using pointers

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

No comments:

Post a Comment