I have created a 2D array, and tried to print certain values as shown below:
int a[2][2] = { {1, 2},
{3, 4}};
printf("%d %d\n", *(a+1)[0], ((int *)a+1)[0]);
The output is:
3 2
I understand why 3
is the first output (a+1
points to the second row, and we print its 0th
element.
My question is regarding the second output, i.e., 2
. My guess is that due to typecasting a
as int *
, the 2D array is treated like a 1D array, and thus a+1
acts as pointer to the 2nd
element, and so we get the output as 2
.
Are my assumptions correct or is there some other logic behind this?
Also, originally what is the type of a
when treated as pointer int (*)[2]
or int **
?