#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *a = "Hello ";
const char *b = "World";
printf("%s", strcat(a, b));
system("PAUSE");
return EXIT_SUCCESS;
}
Asked
Active
Viewed 127 times
1
unwind
- 378,987
- 63
- 458
- 590
Amol Aggarwal
- 2,284
- 2
- 22
- 32
-
3String literals are not modifyable. http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault/1614739#1614739 – AnT Jan 14 '10 at 08:19
2 Answers
7
Because you are writing data at a memory location that you do not own.
Indeed, when running strcat, you are appending the characters of string b right after the characters of string a. But you haven't claimed for the memory after the string a.
Didier Trosset
- 34,718
- 13
- 85
- 119