首页 > 生活

去掉A处strcat与不去掉,s1的结果为什么不同?

更新时间2019-03-10 17:04:19

#include<stdio.h>

#include<string.h>

int main(void){

char s1[]="big ball",s2[]="small tree";

strcpy(s1,s2);

strcat(s2," and boy");//----------------A

puts(s1);

printf("%s ",s2);

return 0; 

 } 

-------------------------------------------

不去:

oy-----------s1

small tree and boy

 

--------------------------------

Process exited after 0.6545 seconds with return value 0

请按任意键继续. . .

去:

small tree------------s1

 

--------------------------------

Process exited after 0.5361 seconds with return value 0

请按任意键继续. . .


你的程序能运行只是幸运,因为下标溢出了

s1,字串长为8,实际空间为9个

而s2长度为10,实际空间为11个

strcpy(s1,s2); //s1无法容纳s2的内容

在C、C++中,下标溢出导致的结果是

“不可预见的”

所以,任何结果都是可能的,没有结果都是正常的

上一篇:想买眼镜框有什么推荐吗?

下一篇:头皮屑多,有什么办法