Can any one explain this code snippet

manojkrishnaks

Disciple
Hi all, i came across this code snippet
Code:
void main()
{
int a[2][3][2]={{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};
printf("\n%d%d%d",a[1]-a[0],a[1][0]-a[0][0],a[1][0][0]-a[0][0][0]);
}

The answer is 3 6 1
can anyone explain why it is 3 6 1
 
Hi all, i came across this code snippet
Code:
void main()
{
int a[2][3][2]={{{1,2},{9,8},{3,7}},{{2,2},{1,4},{5,4}}};
printf("\n%d%d%d",a[1]-a[0],a[1][0]-a[0][0],a[1][0][0]-a[0][0][0]);
}

The answer is 3 6 1
can anyone explain why it is 3 6 1


for the below two what you are getting is the difference of starting address of the array.
it will be always 24 considering size of int as 4 byte.
in the first case the answer is 24/8=3 and in second it is 24/4=6
not sure why it is dividing by 8 in first case.
may be it is treating two numbers together as if it is a single dimensional array
so what ever the value you give 1) and 2) will always give 36
1) a[1]-a[0]
2) a[1][0]-a[0][0]

btw, what is the expected answer for this ? what is the logic for subtracting multi-dimensional array ?

3) a[1][0][0]-a[0][0][0] -> this is valid and you are getting the value of 2-1

the above is what i found during debugging, please correct me if i am wrong
 
for the below two what you are getting is the difference of starting address of the array.
it will be always 24 considering size of int as 4 byte.
in the first case the answer is 24/8=3 and in second it is 24/4=6
not sure why it is dividing by 8 in first case.
may be it is treating two numbers together as if it is a single dimensional array
so what ever the value you give 1) and 2) will always give 36
1) a[1]-a[0]
2) a[1][0]-a[0][0]

btw, what is the expected answer for this ? what is the logic for subtracting multi-dimensional array ?

3) a[1][0][0]-a[0][0][0] -> this is valid and you are getting the value of 2-1

the above is what i found during debugging, please correct me if i am wrong

Yes what ever the value in the array gives
a[1]-a[0]=3
a[1][0]-a[0][0]=6

According to the book right answer is 3 6 1,
when i printed just a[1] and a[0] i got it as -22 and -34
and similary a[1][0] and a[0][0] give -22 and -34


according to your logic
a[1]-a[0] give -22+34=12
when we convert it to int it will give answer 3

- - - Updated - - -

Found some article related to array addressing in http://stackoverflow.com/questions/...n-multi-dimensional-array-column-vs-row-major

Basically in a 3D arrray with row major E.g Arr[3][4][5] when u want Arr[0],it looks for the front slice of the image like above Arr[1] second slice and so on. so Arr[0] means 2D array of size [4][5] ,so every Arr[x] corresponds to x*(4*5) Now about Arr[x][y] can be thought of a 1D array of size [5] whose position is [x][y] from top view in the image above so when when we want Arr[x][y] = x*(4*5)+y*(5) now Arr[x][y][z] is the specific element of Arr[x][y] at depth z Arr[x][y][z]=x*(4*5)+y*5+z now on the basis of size of the data type u want to store in the array,the offset can be multiplied to the size get the address with respect to start
According to my understanding Arr[0]=x*4+5 then it satisfies all the cases properly
 
-edit-
For the answer to OP search for 'Updated' and read from there.
Continue from here if you want to know about how multidimensional arrays are stored in memory in C.
I had separated the answer from this long explanation but consecutive posts are merged.
-edit-


Look at it this way to understand arrays and how they are stored in the memory.

C uses Row-major order for storing multidimensional arrays.
http://en.wikipedia.org/wiki/Row-major_order

1-D Array of int a[4] = {1,2,3,4}
this is stored in a linear manner in the memory starting from a[0] = 1 to a[3] = 4.

Memory locationxx+4x+8x+12
Array addressinga[0]a[1]a[2]a[3]
Value1234

The memory location of a[0] is 'x' which can change but the difference between the two consecutive elements of the int array will always be 4 (sizeof(int) = 4 ). If it was a char the difference would have been 1 (sizeof(char) = 1).



2-D Array of int a[2][2] = {{1,2},{3,4}}

Memory locationxx+4x+8x+12
Array addressinga[0][0]a[0][1]a[1][0]a[1][1]
Value1234


This is stored as two 1-D arrays one after the other. a[0][] followed by a[1][].



3-D Array of int a[2][2][2] = {{{1,2},{3,4}},{{5,6},{7,8}}}
Memory locationxx+4x+8x+12x+16x+20x+24x+28
Array addressinga[0][0][0]a[0][0][1]a[0][1][0]a[0][1][1]a[1][0][0]a[1][0][1]a[1][1][0]a[1][1][1]
Value12345678


This is stored as two 2-D arrays one after the other. a[0][][] followed by a[1][][].

The 2-D arrays are again stored as two 1-D arrays one after the other.a[0][0][] followed by a[0][1][] followed by a[1][0][] followed by a[1][1][].

Answer in the next post. Keeping this as it is.

- - - Updated - - -

The answer lies in how pointer addition/subtraction takes place using implicit sizeof(datatype).

Consider a 1D array int a[2].
suppose a[0] is at memory location x, then a[1] is actually located at a[0] + 4. 4 being sizeof(int).

Even if it was a 1D array of a structure (struct xyz), the n[SUP]th[/SUP] element is always accessed using the base address of a[0] and adding ((n-1)*sizeof(struct xyz)) to it.

Now consider a 2D array int b[2][3].
Here b[0] is actually of the type int [3]. To access b[1] the compiler takes the base address of b[0] and adds the 3*sizeof(int) which is actually 12.
luster had the correct idea when he said that we divide the memory address difference by 4 to get the answer, but that holds true for a 2D int array because sizeof(int) = 4. The same can be applied in the above example and b[1] -b[0] comes out to be 3.


For a 3D array c[2][3][2],
c[0] is of the type int [3][]. The second bracket is blank because it can be anything, in this case it is int [2]. c[1] is calculated using address of c[0] and adding into it 3*sizeof(int [2]) which is 3*8 = 24.

c[0] has 3 elements each of type int[2]. Hence c[1] - c[0] = (Memory difference in bytes)/(sizeof (int[2]).

Generalizing the above statement, the element which comprises of an array can be anything, like a char,int,float,struct,another array etc...

this also gives the answer for c[1][0]-c[0][0] where we divide the memory difference by 4.

I hope this clears the doubt. Thanks to manojkrishnaks for this question which increased my knowledge of C a bit more.
 
Back
Top