Naveen
Naveen

Reputation: 43

Segmentation fault in string concatenation using pointers

I'm trying to concatenate two strings stored in character pointers, but am doing something wrong. Could someone point out what it is, please? Also, I'm not using any built in functions purposely.

int main()
{
    char *a = "abc";
    char *b = "def";
    char *c;

    while(*a != '\0')
    {
        *c = *a;
        a++;
        c++;
    }

    while(*b != '\0')
    {
        *c = *b;
        b++;
        c++;
    }

    *c = '\0';
    c -= 6;

    while(*c!= '\0')
    {
        printf("%c", *c);
        c++;
    }

    return 0;

}

Upvotes: 2

Views: 167

Answers (4)

Foxtrot
Foxtrot

Reputation: 1

You are are getting the error because the pointer c is not initialized.

int main()
{
char *a = "abc";
char *b = "def";
char x ;
char *c = &x; //initializing pointer

while(*a != '\0')
{
    *c = *a;
    a++;
    c++;
}

while(*b != '\0')
{
    *c = *b;
    b++;
    c++;
}

*c = '\0';
c -= 6;

while(*c!= '\0')
{
    printf("%c", *c++);//increment pointer
}
return 0;

}

Upvotes: 0

Steve
Steve

Reputation: 1757

You haven't allocated any memory for c

b and c get memory allocated statically, when you do:

char *a = "abc";
char *b = "def";

But c doesn't have that, so you would need to allocate memory using something like:

char *c = malloc (x);

where x is the total length of the character array you'd need to accomodate the characters you wish to insert (plus 1, for the terminating NULL). You'd also need to remember to free () it somewhere down the line.

As you're not doing any allocation, this line:

*c = *a;

will produce undefined behaviour.

Upvotes: 4

P.P
P.P

Reputation: 121397

c is uninitialized. You must initialize it before copying values. For example:

char *c = malloc(strlen(a) + strlen(b) + 1); /* plus 1 for terminating null byte */
if (!c) {
    perror("malloc");
    exit(EXIT_FAILURE);
}

Other problem is you are not incrementing c when you print:

   while(*c!= '\0')
    {
        printf("%c", *c);
        c++;
    }

Note that this statement

c -= 6;

is fine here but not very readable. You are better off using a temporary pointer to save the initial position of c, so that you don't need to do this.

If you can't use standard strlen function, then it's straight-forward to implement it yourself.

Upvotes: 2

Mr.C64
Mr.C64

Reputation: 42964

char *c;

With this line, you define a variable c that is a pointer to a char. It's clear your idea is to store in it the concatenation of the strings a and b; but, to do that, you need to have some memory available for the result 'a+b' (concatenation) string.

What you have to do is allocating enough memory for the destination string, counting the characters in the two source strings to concatenate, plus the terminating NUL (\0). In your case, you need 3 + 3 + 1 = 7 chars for the result string.

You can either allocate them on the stack, like this:

char result[7];

Or dynamically allocate using malloc(). In this case, you also need to free the memory invoking free().

Upvotes: 2

Related Questions