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
  • 391,730
  • 64
  • 469
  • 606
Amol Aggarwal
  • 2,674
  • 3
  • 24
  • 32

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
  • 36,376
  • 13
  • 83
  • 122
2

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

Dan Olson
  • 22,849
  • 4
  • 42
  • 56
Alon
  • 4,862
  • 19
  • 27