O haver a doubt about multi-dimensional arrays and pointers notation, as bellow.
arr[2][4] = {{1,2,3,4}, {5,6,7,8}};
printf("%d\n", (arr+1));
printf("%d\n", *(arr+1));
Why in the both printf(), the result printed in the screen is the same? Both bring the address of the second array inside of the arr.
The first one I understand because arr is a memory address and so, adding 1 to it adds (the size of the inner array * size of integer).
But the second one I am confused because if I put * before arr+1 it shoudn’t bring the value inside the address of arr+1 ?
>Solution :
(arr)
points to the first memory address of the 2 * 4
array, i.e., arr[0][0]
.
(arr+1)
points to the 2nd 1-D array comprising arr
, i.e., it points to the array arr[1]
.
Now, *(arr+1)
is basically the address of the first element of the 2nd 1-D array comprising arr
, i.e., it points to the element arr[1][0]
.
If you know pointers well, arr[1]
is the same address as arr[1][0]
.
You can confirm that by checking **(arr+1)
, which will output 5
in your case.