1
#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;
}
unwind
  • 378,987
  • 63
  • 458
  • 590
Amol Aggarwal
  • 2,284
  • 2
  • 22
  • 32
  • 3
    String 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 Answers2

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
2

When you are concatenating b to a you are writing into memory you didn't allocate,

Dan Olson
  • 22,101
  • 4
  • 39
  • 56
Alon
  • 4,832
  • 18
  • 27