2

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;

}

1
  • 4
    Uninitialized local non-static variables are left uninitialized by the compiler and run-time environment. Their values will be indeterminate. Dereferencing such an uninitialized pointer will lead to undefined behavior. Now take a long hard look at the code you have, and I'm sure you will figure it out. Commented Oct 16, 2017 at 15:20

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"where x is the total length of the character array you'd need to accomodate" plus one for the nul termnation
2
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().

Comments

2

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.

Comments

0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.