如果我像下面这样定义一个字符缓冲区
char buffer[20] = "foo";
然后
buffer[0] == 'f';
buffer[1] == 'o';
buffer[2] == 'o';
buffer[3] == 0;
关于 buffer[4] 到 buffer[19] 标准是怎么说的。它们也保证被初始化为零吗?
最佳答案
保证将所有剩余元素初始化为零。
引用:
C++03 标准第 6.7.8 节第 21 段:
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
关于c++ - char 缓冲区初始化的标准行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7945756/