bool graph[5][4]
is an array of arrays. It's represented by, effectively, 20 Booleans in a row (possibly with some padding). It can be decayed into bool(*)[4]
, which is a pointer to some number of collections of four Booleans in a row.
On the other hand, bool**
is a pointer to one or more pointers to one or more Booleans. There's an additional layer of indirection, in that the first array contains pointers to the second, as opposed to simply containing the second directly. At the top layer, it's easy to convert from T[]
to T*
, since that's effectively just taking an address, but if it's not the top layer of the array, then it would require iterating over the array to change all of the constituents. And that's more or less what you need; an array of pointers.
bool** graph = calloc(5, sizeof(bool*))
for (int i = 0; i < 5; i++) {
graph[i] = calloc(4, sizeof(bool));
// Initialize graph[i] here ...
}
Then you'll need to free it all at the end.
for (int i = 0; i < 5; i++) {
free(graph[i]);
}
free(graph);
Of course, if you have control over the signature of the target function, you may consider just changing it to take a bool*
. Then you can work with one-dimensional arrays and just keep track of the number of "rows" and "columns", which will better align your memory and avoid issues like this.