in Programming in C
698 views
2 votes
2 votes
main(){

int a[2][3][2]={ { {1,2},{9,8},{3,7} },{ {2,3},{1,4},{5,4} } };

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

}

A) 3 3 1

B) 3 6 1

C) 6 6 1

D) 1 1 1

Plz explain
in Programming in C
by
698 views

2 Answers

3 votes
3 votes
a[1][0][0]-a[0][0][0]=2-1=1
a[1][0]-a[0][0]=(address of a[1][0][0]- address of a[0][0][0])/size(int)=6*sizeof(int)/sizeof(int)=6;
(note: here a[1][0] is of type int *(i.e, starting address of 1D array or Integer pointer ) hence we divide by sizeof(int) )
a[1]-[0]=(address of a[1]-address of a[0])/ sizeof(int[2])=6*sizeof(int)/sizeof(int[2])=3
(note: here a[1] is of type int (*)[2](i.e, starting address of 2D array or pointer to a Integer array int[2]) hence we divide by sizeof(int [2]) )
Hence the answer is B
edited by

4 Comments

thanks for correcting me sir.. have edited my answer
0
0
in the case when a[1] is a pointer to a 3 dimensional array we would divide by 3 to get the difference between 2 pointers?
0
0
edited by
a  has starting address of 3D array or ID array of int [3][2]
a[0] has starting address of 2D array or ID array of  int[2]
a[0][0] has starting address of 1D array or ID array of  integers
a[0][0][0] is a integer.
So
a points to a element of sizeof int[3][2]
a[0] points to element of sizeof int[2]
a[0][0]points to element of sizeof int
0
0
0 votes
0 votes
option D ...

Related questions