int arr[3][2]={{1,2},{3,6},{8,6}};
printf("%d %d %d",arr[0][3],arr[1][2],arr[2][0]);
output of this program is coming as 6 8 8 can someone please explain me how is this happening
int arr[3][2]={{1,2},{3,6},{8,6}};
printf("%d %d %d",arr[0][3],arr[1][2],arr[2][0]);
output of this program is coming as 6 8 8 can someone please explain me how is this happening
The values {{1,2},{3,6},{8,6}}; are stored in the consecutive memory location.
Assume this way:
Let say arr location is : 100, size of integer is 4
Location 100 : 1
Location 104 : 2
Location 108 : 3
Location 112 : 6
Location 116 : 8
Location 118 : 6
When you are accessing
arr[0][3] = arr[0] = 100. arr[0][3] = 100 + (3*4) = 112 location Ie: value 6.
arr[1][2] = arr[1] = 108. arr[1][2] = 108 + (2*4 ) = 116 loation Ie: value 8.
arr[2][0] = arr[2] = 116. arr[2][0] = 116 + 0 = 116 location. Ie: value 8.
Hence it get printed 6, 8, 8.
Hope it helps.
a[0] will be pointing at a+0 i.e at 1(a[0][0]) so now adding 3 from there will be resulting in moving arr to 6.
now a[1][2], a[1] points at '3'(a[1][0]) so adding 2 to it results in arr pointing to 8.
similary for a[2][0], a[2] will be pointing at '8' (a[2][0]).
This result is because the numbers in this array is stored in memory continuously row by row.