2
#include <stdio.h>

int main()
{
    int a [2][3][2]={{{1,2},{3,4},{5,6}},{{5,8},{9,10},{11,12}}};

    printf("%d\n%d\n%d\n",a[1]-a[0],a[1][0]-a[0][0],a[1][0][0]-a[0][0][0]);
    return 0;
}

The output is 3 6 4. Can anyone explain to me the reason for this? How come a[1]-a[0]=3 and a[1][0]-a[0][0]=6 and how a[] and a[][] interprets in a 3-dimensional array?

3 Answers 3

2

It might help if you understand how an array like yours is laid out in memory:

+------------+   Low address    +---------+   Low address    +------+
| a[0][0][0] |                  | a[0][0] |                  | a[0] |
| a[0][0][1] |                  |         |                  |      |
| a[0][1][0] |                  | a[0][1] |                  |      |
| a[0][1][1] |                  |         |                  |      |
| a[0][2][0] |                  | a[0][2] |                  |      |
| a[0][2][1] |                  |         |                  |      |
| a[1][0][0] |                  | a[1][0] |                  | a[1] |
| a[1][0][1] |                  |         |                  |      |
| a[1][1][0] |                  | a[1][1] |                  |      |
| a[1][1][1] |                  |         |                  |      |
| a[1][2][0] |                  | a[1][2] |                  |      |
| a[1][2][1] |                  |         |                  |      |
+------------+   High address   +---------+   High address   +------+

Then it helps to know that the difference you get is in multiples of the type. So for a[0] and a[1] the type is int[3][2] and there are three of those multiples between a[0] and a[1].

Same for a[0][0] and a[1][0], the type is int[2], and the difference is six int[2] units between a[0][0] and a[1][0].


To elaborate a little: Between a[0] and a[1] you have a[0][0], a[0][1] and a[0][2]. Three entries.

Between a[0][0] and a[1][0] you have a[0][0][0], a[0][0][1], a[0][1][0], a[0][1][1], a[0][2][0] anda[0][2][1]. Six entries.

Sign up to request clarification or add additional context in comments.

5 Comments

please elaborate this
@BakulMittal a[0] and a[0][0] are pointers, so when you substract them with a[1] and a[1][0], the result is difference between two memory locations. a[0][0][0] and a[1][0][0] are values in the array, so when you substract them, you get difference between two values.
can you please explain how compiler interpret it through addressing
@BakulMittal Remember that arrays naturally decays to pointers to their first element. So a[0] decays to &a[0][0]. What the compiler is doing is subtracting the pointers for &a[0][0] from &a[1][0], and dividing that difference with the size of the type (which is int[3][2]).
@ sir Joachim Pileborg the address int [3][2]=(3*2+2)*4 bytes and int[2]= 2*4 bytes so how its interpreted
0

At the point of address, a[1] and a[1][0] are the same value. And a[0] and a[0][0] are same value.

But the types are different.

a[1][0] and a[0][0] are int *, from a[0][0] to a[1][0], there are 6 int.

And from a[1] to a[0], there are 3 {x, y}.

a[1][0][0] and a[0][0][0] are int, a[1][0][0]-a[0][0][0] = 5 - 1 = 4.

2 Comments

can u please elaborate it
The Add and subtract of pointers is multiple of types. For example, int *a = 100; int *b = 0; a - b = 25, 25 * sizeof(int)
0

In C, a multi-dimensional array is conceptually an array whose elements are also arrays. So if you do:

int array[2][3]; Conceptually you end up with:

array[0] => [0, 1, 2]
array[1] => [0, 1, 2]

int array[2][3][2]; ...will give you a structure like:

array[0] => [0] => [1, 2]
            [1] => [3, 4]
            [2] => [5, 6]
array[1] => [0] => [5, 8]
            [1] => [9, 10]
            [2] => [11, 12]

a[1]-a[0] => will give difference you get is type of unit. a[0] and a[1] is int and there are three unit between them.similarly for the second part

a[1][0]-a[0][0]=6

number of combination for between a[0][0] and a[1][0] is 6.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.