我试图理解字符数组与下面的代码更 困惑字符数组
int main() {
char test[] = "hello";
char here[4] = "ola";
char bon[] = { 'w','o','r','d' };
char word[4] = { 'z','f','f','z' };
std::cout << test << std::endl;
std::cout << here << std::endl;
std::cout << bon << std::endl;
std::cout << word << std::endl;
return 0;
}
输出
hello
ola
wordhello
zffzwordhello
为什么它给了我这个输出不
hello
ola
word
zffz
您在这里末尾缺少一个''\ 0''字符:'char bon [] = {'w','o',' r','d'} ;.' –
和“word”一样。 –
如果将字符串传递给输出例程,则该例程不知道该字符串有多长。粗略地说:它继续打印,直到达到第一个''\ 0''字符。 –