Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcat implementation

Tags:

c

strcat

I tried to implement the strcat by myself, and I found the strcat implementation from Wiki like this......but when I use it, there is segmentation fault.

What's wrong with the code below?

char *
strcat(char *dest, const char *src)
{
    size_t i,j;
    for (i = 0; dest[i] != '\0'; i++)
        ;
    for (j = 0; src[j] != '\0'; j++)
        dest[i+j] = src[j];
    dest[i+j] = '\0';
    return dest;
}
like image 957
skydoor Avatar asked Mar 21 '10 20:03

skydoor


1 Answers

the code is okay.

Looks like you have a problem in the calling code.

Did you remember to allocate enough memory for the target string?

like image 153
Pavel Radzivilovsky Avatar answered Sep 29 '22 04:09

Pavel Radzivilovsky