1

I have written the following program (it is given as an example in one of the best text books). When I compile it in my Ubuntu machine or at http://www.compileonline.com/compile_c_online.php, I get "segmentation fault"

The problem is with while( *p++ = *str2++)

I feel it is a perfectly legal program. Experts, please explain about this error.

PS: I searched the forum, but I found no convincing answer. Some people even answered wrong, stating that *(unary) has higher precedence than ++ (postfix).

#include <stdio.h>
#include <string.h>

int main()
{
char *str1= "Overflow";
char *str2= "Stack";

char *p = str1;
while(*p)
++p;

while( *p++ = *str2++)
;

printf("%s",str1);

return 0;
}

Thanks

3 Answers 3

3

str1 and str2 point to string literals. You aren't allowed to modify those. Even if you could, there isn't enough memory allocated for the string to hold the characters you're trying to append. Instead, initialize a sufficiently large char array from a string literal:

char str1[14] = "Overflow";
Sign up to request clarification or add additional context in comments.

4 Comments

This will give "lvalue required as increment operand" error
@Aravindmavnoor: Did you try to do str1++? You still need the p variable. You can't increment an array.
@Aravindmavnoor: str1++ is the thing you're not supposed to do, because it won't work. You need to declare p as a char * and initialize it to str1.
i haven't read ISO/IEC standard on C. Below link is useful stackoverflow.com/questions/2589949/…
1

I feel it is a perfectly legal program.

Unfortunately, it is not. You have multiple, severe bugs.

First of all, you are creating pointers to string literals char *str1= "Overflow"; and then you try to modify that memory. String literals are allocated in read-only memory and attempting to write to them results in undefined behavior (anything can happen).

Then you have while(*p) ++p; which looks for the end of the string, to find out where to append the next one. Even if you rewrite the pointers to string literals into arrays, you don't have enough free memory at that location. You must allocate enough memory to hold both "Overflow" and "Stack", together with the string null termination.

You should change your program to something like this (not tested):

#include <stdio.h>

int main()
{
  char str1[20] = "Overflow"; // allocate an array with enough memory to hold everything
  char str2[]  = "Stack"; // allocate just enough to hold the string "Stack".

  char *p1 = str1;
  char *p2 = str2;
  while(*p1)
    ++p1;

  while(*p1++ = *p2++)
    ;

  printf("%s",str1); // should print "OverflowStack"

  return 0;
}

Or of course, you could just #include <string.h> and then strcat(str1, str2).

4 Comments

string.h, with an r in it.
I accept your answer and it works fine. My doubt is I am reading "A Book on C by Al Kelly and Ira Pohl" in which author says that strcat can be implemented without using string.h in many ways and one way is as shown in my question which gives error. Why did author mislead ?
i haven't read ISO/IEC standard on C. Below link is useful stackoverflow.com/questions/2589949/…
Author of A book on C is correct.IT was my mistake to add 2 statements containing string literals. Now my doubts is cleared
0

Because you are crossing the string boundary of Overflow (str1) is why you are getting sigsegv.

str1 does not have enough memory allocated to accomodate beyond Overflow.

4 Comments

Why should compiler do that?
@Aravindmavnoor: If you expect C to do convenient things for you, you will usually be disappointed.
C compiler will do exactly what you tell it to do.
it is nothing to do with compiler. I cannot change pointer to string literals as in the program above

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.