586 views
0 votes
0 votes
main()

{

int arr[2][3][2] ={{{1,2}{3,4}{5,6}} , {7,8}{9,10},{11,12}}};

printf("%d%d",a[1]-a[0],a[1][0]-a[0][0]};

return 0;

}

assume int is of 2 bytes

1 Answer

Best answer
3 votes
3 votes

Here we need to understand what a[0] refers to a[0][0] refers to..

a)  When we simply mention 'a' , it is the base address of the entire array and hence points to the entire 3-D array..

b) When we write a[0] , it means there is one level of dereferencing , hence we descend to refer to only a 2-D and specifically through a[0] , we refer to 1st 2-D array..

c) When we write a[0][0] , it refers to first 1D array of 1st 2D array..

d) When we write a[0][0][0] , it directly gives the value of the first element of 1st 1D array of 1st 2D array..

Having said that , now

a) If we assign say pointer p = a[0][0] and do p++ , this will lead to point to second element of 1st 1D array of 1st 2D array itself..Thus element wise traversal occurs if use the address of a[0][0] i.e. in steps of 1 element at a time.

b) If we assign say pointer q = a[0] and do p++ , this will lead to point to second 1D array instead..Hence using a[0] type of address in 3D array will lead to traversal in units of 1D array..

Thus a[1] - a[0] will lead to 3 as a[1] is the base address of second 2D array and a[0] is of first 2D array.And hence to reach 2nd 2D array , we have passed 3 1D arrays of 1st 2D array , thereby giving the result as 3..

On the other hand , a[1][0] - a[0][0] will lead to 6 as here traversal is element wise as mentioned in a) point..

Hence the correct answer should be 36..

selected by

Related questions

6 votes
6 votes
2 answers
1
Abhisek Tiwari 4 asked Oct 20, 2018
2,175 views
int arr [3] ;print(arr -arr[0],arr [0]-arr[0][0])P.s :Sorry i can't able to paste the question but this is main Context.
3 votes
3 votes
2 answers
3
Anjan asked Jan 8, 2018
6,288 views
Consider 3 dimensional Array A[90] [30] [40] stored in linear array. If the base address starts at 10, The location of A [20] [20] [30] in case of RMO and CMO are _______...
4 votes
4 votes
3 answers
4
Hira Thakur asked Nov 19, 2017
874 views
#include<stdio.h int main() { int arr [3] ={{{2,4},{7,8},{3,4},},{{2,2},{2,3},{3,4},}}; print("\n%d", (*arr+1)+2+7); return 0; } plz explain this program, internally how ...