如果我有以下情况:
char test[10] = "#";
test[1]
到 test[9]
是否保证为 \0
?还是只有 test[1]
保证为 \0
?
最佳答案
这个定义
char test[10] = "#";
等价于
char test[10] = { '#', '\0' };
即数组的两个元素由初始化器显式初始化。数组的所有其他元素将被初始化为零,这意味着它们将被隐式设置为 '\0'
根据 C++ 标准(第 8.5.2 节字符数组)
3 If there are fewer initializers than there are array elements, each element not explicitly initialized shall be zero-initialized (8.5).
关于c++ - 将char数组初始化为字符串值,未初始化的索引是否设置为null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25362074/