2

How can I declare a two-dimensional array of pointers and initialize it by null pointers in c++ ?

I tried to do this

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

1 Answer 1

5

You can aggregate initialize the array with empty initializer list,

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

(emphasis mine)

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).

Then all the elements of arr (i.e. the sub-array int* [30]) would be 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.

At last elements with type int* (which is built-in type) are all zero-initialized to null pointers.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.