请看下面代码:
结果:
char s[12] = {'a','b','c','a','b','c','a','b','c','a','b','c'};
cout<<"strlen(s):"<<strlen(s)<<endl;
cout<<"s:"<<s<<endl;
运行结果:
strlen(s):15
s:abcabcabcabc?
分析:
为什么长度为15?不得而知,但是可以通过加一个‘\0’结束,即可:
char s[12] = {'a','b','c','a','b','c','a','b','c','a','b','\0'};
// char s[2];
cout<<"strlen(s):"<<strlen(s)<<endl;
cout<<"s:"<<s<<endl;
结果:
strlen(s):11
s:abcabcabcab