bool retval[4][4] = new bool[4][4];
The return type is a pointer, you've to assign it to a pointer variable and not an array variable.
My understanding was that a [][] array was the same thing as a **. Is this not true? Why can't I return it as a **?
No, because array decay isn't recursive. When you do new bool[4][4]
, you'd get bool (*) [4]
as the returned pointer (only first level has decayed here).
What is the proper way to call new for a two-dimensional array?
This
bool (*retval) [4] = new bool[4][4];
should work. Each element is accessed as usual retval[i][j]
, only that the type of retval
is a bit strange for the uninformed; it is a pointer to an array of four booleans. However it's safer to the below alternative (where both dimensions are lost) since the compiler can check the length as it is part of the type; however, the second dimension is lost in this method too. If you want both dimensions to be preserved, use std::array<std::array<bool, 4>, 4>
. The caveat in both this and the std::array
methods is that they can be employed only when the first dimension is unknown, but all remaining dimensions are known at compile-time; this is for declaring the pointer to an array. The below method can be used even when all dimensions are unknown at compile-time.
This alternative would be using an array of pointers and impregnating each pointer in this array with an array of booleans.
bool** ptrs = new bool*[4];
for (size_t i = 0; i < 4; ++i)
ptrs[i] = new bool[4];
This allows you to return as bool**
. Here too, new bool*[4]
decays at the first level and returns a bool**
pointer. However, if both dimensions are unknown, the recommended (idiomatic) way of doing it is to use std::vector
.
Recommended Reading: How do I use arrays in C++?