Reputation: 11943
I am trying to compile the following code:
bool **copyBools( bool const * const * const input )
{
bool retval[4][4] = new bool[4][4];
for( int i=0; i<4; ++i )
{
for( int j=0; j<4; ++j )
{
retval[i][j] = input[i][j];
}
}
return retval;
}
However, it doesn't compile and gives these errors. The first error is on the bool retval
line and the second error is on the return
line:
error: array must be initialized with a brace-enclosed initializer
error: cannot convert ‘bool (*)[4]’ to ‘bool**’
My understanding was that a [][]
array was the same thing as a **
. Is this not true? What is wrong with this code? What is the proper way to call new for a two-dimensional array? Why can't I return it as a **
?
Upvotes: 1
Views: 80
Reputation: 32914
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++?
Upvotes: 2
Reputation: 96810
You need to use a double pointer here:
bool** retval = new bool*[4];
for( int i=0; i<4; ++i )
{
retval[i] = new bool[4];
for( int j=0; j<4; ++j )
{
retval[i][j] = input[i][j];
}
}
Note: You can just use a multi-dimensional vector here too,
Upvotes: 0