下面的代码buf的输出并非是"helloworld",而是"屯屯屯屯屯妄膮\x14y沞helloworld".
strcat是将前者字符串的'\0'去掉,加上后面的字符串.而malloc之后不进行memset操作,buf的值实际上是乱码,而非空字符串.

char *buf = (char*)malloc(11); strcat(buf, "helloworld");

正确的代码应该是如下的那样:

char *buf = (char*)malloc(11);
memset(buf, 0, 11);
strcat(buf, "helloworld");

相关文章:

  • 2021-05-28
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2021-05-07
猜你喜欢
  • 2021-06-02
  • 2022-02-02
  • 2021-10-02
  • 2021-06-07
  • 2022-12-23
  • 2021-07-22
相关资源
相似解决方案