gpt4 book ai didi

c++ - 带有空指针的二维指针数组 C++

转载 作者:行者123 更新时间:2023-12-01 14:40:09 34 4
gpt4 key购买 nike

如何在 C++ 中声明一个二维指针数组并通过空指针初始化它?

我尝试这样做

int *arr[20][30]= nullptr;

最佳答案

您可以aggregate initialize具有空初始值设定项列表的数组,

int *arr[20][30] {}; 
// or
int *arr[20][30] = {};

(强调我的)

If the number of initializer clauses is less than the number of members and bases (since C++17) or initializer list is completely empty, the remaining members and bases (since C++17) are initialized by their default member initializers, if provided in the class definition, and otherwise (since C++14) by empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates).

那么 arr 的所有元素(即子数组 int* [30])将为 aggregate-initialized如果初始值设定项列表也为空,则 int* 类型的子数组的所有元素将为 value-initialized ,

otherwise, the object is zero-initialized.

最后,int* 类型(内置类型)的元素都被零初始化为空指针。

If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.

关于c++ - 带有空指针的二维指针数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59367088/

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