Here's the code that I am trying to work with.
int main(void) {
int array[2][5];
for (int ** p = array; p < array + 2; p++) {
*((*p) + 2) = 20;
}
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 5; col++) {
printf("%d ", array[row][col]);
}
printf("\n");
}
return EXIT_SUCCESS;
}
And this is the output given out by the compiler.
main.c:11:21: warning: initialization of 'int **' from incompatible pointer type 'int (*)[5]' [-Wincompatible-pointer-types]
11 | for (int ** p = array; p < array + 2; p++) {
| ^~~~~
main.c:11:30: warning: comparison of distinct pointer types lacks a cast
11 | for (int ** p = array; p < array + 2; p++) {
| ^
main.exe
make: *** [Makefile:7: main] Error -1073741819
I clearly don't understand type casting when it comes to multidimensional arrays. It would really help if you could explain why this doesn't work.
Omitting the first for loop and doing things manually like this do seem to give out the intended result.
*((*array) + 2) = 20;
*(*(array + 1) + 2) = 20;
I can't get the hang of "int (*)[5]".