Why a 2-d address of following boolean "mat" can not be passed like this to a function?
void generate(bool arr) {
......;
......;
}
int main() {
bool mat[3][3];
generate(mat);
return 0;
}
Why a 2-d address of following boolean "mat" can not be passed like this to a function?
void generate(bool arr) {
......;
......;
}
int main() {
bool mat[3][3];
generate(mat);
return 0;
}
Try following example then attention to bellow descriptions :
const int N = 3;
void generate(bool arr[][N]) {
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
std::cout << std::boolalpha << arr[i][j] << " ";
}
std::cout << std::endl;
}
}
int main() {
// initialize array
bool mat[N][N] = { {1, 0, 1},
{0, 1, 0},
{1, 0, 1} };
generate(mat);
return 0;
}
The output :
true false true
false true false
true false true
Notice : There are 3 ways to pass a 2-d array to a function exactly according (this answer) :
The function parameter is a 2-d
array :
bool arr[10][10];
void Func(bool a[][10])
{
// ...
}
Func(arr);
The function parameter is a array of pointers :
bool *arr[10];
for(int i = 0;i < 10;i++)
arr[i] = new bool[10];
void Func(bool *a[10])
{
// ...
}
Func(arr);
The function parameters is as pointer
to pointer
:
bool **arr;
arr = new bool *[10];
for(int i = 0;i < 10;i++)
arr[i] = new bool[10];
void Func(bool **a)
{
// ...
}
Func(arr);