@2501 has already answered your question, but, since you want the modified array to be reflected to the main function, you don't actually need a pointer to the array (which will complicate things more)! Just pass the array directly as you'll get the expected results!
Why, you ask?
Short answer: In C, arrays are passed by reference.
Long answer:
Always keep in mind that when you use the name of an array, it gets converted to a pointer to its first element†. This is commonly referred to as "array decay".
Coming back to your code, The diagram of bool matrix[rows][cols];
would be:
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| matrix[0][0] | matrix[0][1] | matrix[0][2] | ... | matrix[0][cols - 1] |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| matrix[1][0] | matrix[1][1] | matrix[1][2] | ... | matrix[1][cols - 1] |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| ... | ... | ... | ... | ... |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
| | | | | |
| matrix[rows - 1][0] | matrix[rows - 1][1] | matrix[rows - 1][2] | ... | matrix[rows - 1][cols - 1] |
| | | | | |
+---------------------+---------------------+---------------------+---------------------+---------------------+
From the above diagram, it is clear that the first element of
bool matrix[rows][cols];
is the first subarray matrix[0][0]
to matrix[0][cols - 1]
. So what happens here is that the address of this subarray is being passed to the function. This is of type bool (*)[cols]
. This would mean that
void func(int rows, int cols, bool matrix[rows][cols])
would work the same way as
void func(int rows, int cols, bool (*matrix)[cols])
So, for example, if you wanted to write to the third slot of the second subarray of matrix
, you can use matrix[1][2] = WHATEVER;
and the changes made from the function would also affect to the caller since the address was passed.
†: There are a few exceptions where array "decay" doesn't occur. See Exception to array not decaying into a pointer?