c++ - char 缓冲区初始化的标准行为

标签 c++ char

如果我像下面这样定义一个字符缓冲区

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/

相关文章:

c - atoi 似乎不适用于我的程序

c: 将 char 值类型转换为 unsigned short

c++ - 如何在 C++ 中将值 char 数组的类型更改为 int?

C - 找到一个单词并获取接下来的两个单词

c++ - 我正在尝试从 C++ 中的一个句子中打印单词,但它给了我段错误(核心转储)

c++ - Windows 事件日志中没有类别

c++ - 十六进制数字系统中的 POINTER 值

c++ - unique_ptr的继承和dynamic_cast

c++ - 在 C++ 中将 uint8_t 数组转换为位集

c++ - ruby 程序 : Trailing zeros in the nth Power of a numbers factorial