带有空指针的二维指针数组

2D array of pointers c++ with null pointers

如何在 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 with empty initializer list too, then all the elements of sub-array with type int* would be 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.