I used the below program to access 2D arrays using pointers.
#include<stdio.h>
void main()
{
int num[3][2]={ {00,01},{10,11},{20,21} };
int i,j;
printf("-----------------------------------");
/* Treating 2d array as 1d array of each row */
for(i=0;i<3;i++)
{
printf("\nThe Address(&num[%d]) is %u \n \n<= WHICH IS THE SAME AS =>\n \n(num[%d]) %u \t",i,&num[i],i,num[i]);
printf("[*(num+%d)] %u \t [(num+%d)] %u ",i,*(num+i),i,(num+i));
printf("\n The Value is %d \n",*num[i]);
printf("\n -----------------------------------");
}
}
And this is the Output:
-----------------------------------
The Address(&num[0]) is 2140353424
<= WHICH IS THE SAME AS =>
(num[0]) 2140353424 [*(num+0)] 2140353424 [(num+0)] 2140353424
The Value is 0
-----------------------------------
The Address(&num[1]) is 2140353432
<= WHICH IS THE SAME AS =>
(num[1]) 2140353432 [*(num+1)] 2140353432 [(num+1)] 2140353432
The Value is 10
-----------------------------------
The Address(&num[2]) is 2140353440
<= WHICH IS THE SAME AS =>
(num[2]) 2140353440 [*(num+2)] 2140353440 [(num+2)] 2140353440
The Value is 20
-----------------------------------
I understood what happens in the program and I know that *(num+i) is used to access each row's address.
But, why both *(num+i) and (num+i) point to the same address?
To access the value at that particular row (i'th row) we use **(num+i) which makes sense because *(num+i) points to the 1st 1D array's row's address and we can use another indirection operator to dereference that pointer.
But how come both *(num+i) and (num+i) point to the same address?
Is this compiler dependent? Or some undefined behavior?
Please provide as much information as possible.