代码如下,请问为什么会输出“Hello.Don't print me!”?
#include <stdio.h>
int main(int argc, char const *argv[])
{
char s1[] = "Don't print me!";
char s2[6] = "Hello.";
printf(s2);
return 0;
}
代码如下,请问为什么会输出“Hello.Don't print me!”?
#include <stdio.h>
int main(int argc, char const *argv[])
{
char s1[] = "Don't print me!";
char s2[6] = "Hello.";
printf(s2);
return 0;
}
因为s2数组溢出了,导致数组最后缺少字符串结束符,当用字符串输出时,会一直输出到内存中遇到第一个字节0为止。可能s1的内存空间正好在s2的后面