gpt4 book ai didi

时间:2019-03-17 标签:c++c-style零初始化{0}

转载 作者:行者123 更新时间:2023-12-02 07:59:57 25 4
gpt4 key购买 nike

https://en.cppreference.com/w/cpp/language/zero_initialization显示零初始化发生在以下场景中:

int array[32] = {};

;但从未提及此事:

int array[32] = { 0 };

后者是否也会在 C++ 中对整个数组进行零初始化,还是仅对第一个元素进行零初始化?如果是这样,那么结构体也是如此吗?

最佳答案

ISO/IEC N489 §9.4.1

  • (5) For a non-union aggregate, each element that is not an explicitly initialized element is initialized as follows:

    • (5.1) If the element has a default member initializer (11.4), the element is initialized from that initializer.

    • (5.2) Otherwise, if the element is not a reference, the element is copy-initialized from an empty initializer list (9.4.4).

    • (5.3) Otherwise, the program is ill-formed.

§9.4.4

  • (3.11) Otherwise, if the initializer list has no elements, the object is value-initialized.
标量的

值初始化导致其零初始化。因此,第二个元素仅显式初始化为 0 array[0],其余元素将被零初始化。

<小时/>

因此,下面的代码

int array[4] = {13};

用值初始化数组

{13, 0, 0, 0}

关于时间:2019-03-17 标签:c++c-style零初始化{0},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59913308/

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