gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:57:02 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com