开发者

why is this code causing runtime error?

开发者 https://www.devze.com 2022-12-16 14:16 出处:网络
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *a = \"Hello \";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char *a = "Hello ";
  const char *b = "World";

  printf("%s", strcat(a, b));
  system("PA开发者_如何学GoUSE");

  return EXIT_SUCCESS;
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消