I don't understand why am i getting this error. Is it not the correct way of array initialization?
#include <iostream>
using namespace std;
int main()
{
bool **arr = new bool *[5]; //rows
for (int i = 0; i < 5; i )
{
arr[i] = new bool[5]; //col
}
arr[5][5] = {
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
};
}
CodePudding user response:
If your array sizes are known statically, at compile time, you can use std::array<T, N>
:
#include <array>
std::array<std::array<bool, 5>, 5> arr = {{
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true}
}};
CodePudding user response:
In this statement
arr[5][5] = {
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
};
you are trying to assign a non-existent object (the valid range of indices for the both dimensions of the array is [0, 5)
) of the type bool
with an initializer list that contains more than one initializers.
So this statement must be removed.
Instead you could write in the for loop
for (int i = 0; i < 5; i )
{
arr[i] = new bool[5] {false,true,false,true,true};
}
Here is a demonstration program.
#include <iostream>
int main()
{
const size_t N = 5;
bool **arr = new bool *[N]; //rows
for ( size_t i = 0; i < N; i )
{
arr[i] = new bool[N] {false,true,false,true,true};
}
std::cout << std::boolalpha;
for ( size_t i = 0; i < N; i )
{
for ( size_t j = 0; j < N; j )
{
std::cout << arr[i][j] << ' ';
}
std::cout << '\n';
}
for ( size_t i = 0; i < N; i )
{
delete [] arr[i];
}
delete [] arr;
}
The program output is
false true false true true
false true false true true
false true false true true
false true false true true
false true false true true
Another approach is to allocate initially a two-dimensional array.
Here is a demonstration program.
#include <iostream>
int main()
{
const size_t N = 5;
bool ( *arr )[N] = new bool [N][N]
{
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true}
};
std::cout << std::boolalpha;
for ( size_t i = 0; i < N; i )
{
for ( size_t j = 0; j < N; j )
{
std::cout << arr[i][j] << ' ';
}
std::cout << '\n';
}
delete [] arr;
}
The program output is the same as shown above
false true false true true
false true false true true
false true false true true
false true false true true
false true false true true
Pay attention to that instead of manually allocating arrays you could use standard container std::vector<std::vector<bool>>
.
Here is one more demonstration program.
#include <iostream>
#include <vector>
int main()
{
std::vector<std::vector<bool>> arr =
{
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true},
{false,true,false,true,true}
};
std::cout << std::boolalpha;
for ( const auto &row : arr )
{
for ( const auto &item : row )
{
std::cout << item << ' ';
}
std::cout << '\n';
}
}
Again the output is
false true false true true
false true false true true
false true false true true
false true false true true
false true false true true