0

This throws an error id returned 1 error status. The loop is working but I cant print the final concatenated string. When i try to print the final string it does nothing.

char str1[50], str2[50], str[100];
char *p1, *p2, *p3;
int i = 0, j = 0;

p1 = &str1[0];
p2 = &str2[0];
p3 = &str[0];

printf("enter a string:");
gets(str1);

printf("enter a string:");
gets(str2);

while (i <= strlen(str1) + strlen(str2)) {
    if (i != strlen(str1)) {
        *(p3 + i) = *(p1 + i);
        i++;
    } else {
        *(p3 + i) = *(p2 + j);
        j++;
    }
9
  • 2
    Welcome to Stack Overflow! Why is the gets function so dangerous that it should not be used? Commented Apr 25, 2019 at 12:35
  • "but i cant print the final concatenated string" - why not show the code which does the printing? Also, are you adding a NUL terminator correctly? Commented Apr 25, 2019 at 12:36
  • and where are you printing, exactly? Commented Apr 25, 2019 at 12:36
  • Welcome to Stack Overflow! Instead of describing your code which did not work, can you show it? Commented Apr 25, 2019 at 12:36
  • Welcome to Stack Overflow! We need to see a minimal reproducible example for this problem. Also: idownvotedbecau.se/nomcve Commented Apr 25, 2019 at 12:37

1 Answer 1

1

This check here is wrong:

if(i!=strlen(str1))

When i is bigger than strlen(str1) it will get false again, but it is supposed to stay true. Change it to this:

if (i < strlen(str1))

Furthermore, you're not increasing i in the else block. This is going to cause an endless loop with ever-growing j, at least until the undefined behavior will cause your program to exit, possibly through an access violation. Try this instead:

else {
    *(p3 + i) = *(p2 + j);
    i++; // increase i in both cases
    j++;
}

Or better yet, make it unconditional. For instance, you could take the i++ out of the body change the loop to this:

for (i = 0; i <= strlen(str1) + strlen(str2); i++)

Also, may I interest you in the array subscript notation? Instead of this:

if (i < strlen(str1)) {
    *(p3 + i) = *(p1 + i);
}

Do this:

if (i < strlen(str1)) {
    p3[i] = p1[i];
}

It's much cleaner this way and people who will work with your code will appreciate it.

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

4 Comments

// increase i in both cases..rather, make it unconditional.
And please do suggest the array subscript notation: p3[i] & p2[j].
@SouravGhosh good idea. I edited the answer to include suggesting a for loop with an i++, I guess that would be a clean approach.
@machine_1 Great suggestion, added it to the answer. I have to admit, looking at the *(p3 + i) = *(p1 + i) construct takes me some time to figure out what's going on.

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.