# understanding 3D arrays

``````#include <stdio.h>
int main() {
int a ={
{
{1,2},{3,4},{5,6}
},
{
{5,8},{9,10},{11,12}
}
};

printf("%d\n%d\n%d\n",
a - a, a - a, a - a);

return 0;
}
``````

the output is `3`, `6`, `4`. can anyone explain how `a - a== 3` and `a - a == 6` and how `a[]` and `a[][]` are interpreted in 3D-arrays?

A 2D array is an array of arrays. a 3D array is an array of arrays of arrays. in this case we have an array of 2 arrays each of which is of 3 arrays each of which is 2 elements long.

recall that, in C, when an array name used by itself in a place of a pointer, it decays to a pointer to the first element in that array. another thing is that the address of an array is the same address as the address of the first element in that array.

by that, all of the following pointers point to the same location (though they mean different things):

• `a` - a pointer to the first element in the 3D-array `a` (`&a`).
• `&a` - a pointer to the whole 3D-array `a`.
• `a` - a pointer to the first element in the 2D-array `a` (`&a`).
• `&a` - a pointer to the whole 2D-array `a`
• `a` - a pointer to the first element in the array `a` (`&a`)
• `&a` - a pointer to the whole array `a`

similarly, the following point to the same location (though mean different things)

• `a` - points to the first element in the 2D-array `a` (`&a`)
• `&a` - points to the whole 2D-array `a`
• `a` - points to the first element in the array `a` (`&a`)

we also know that array elements live in contiguous locations in memory. given the next piece of code:

``````int arr[] = {1, 2};
int* p0 = &arr; // points to the first element
int* p1 = &arr; // points to the second element

printf("%u\n", p1 - p0); // outputs 1 (not 4)
``````

although `p1` is 4 bytes after `p0` (because an `int` is 4 bytes long on the appliance), the result of subtracting `p0` from `p1` gives `1` not `4` — meaning 1 unit of the size of the type of values pointed to by these pointers (here the pointers point to `int`s, so a unit is 4 bytes long).

between `a` and `a` there are 6 elements (a total of 24 bytes). that means that if `a` points to location `100`, `a` should be pointing to location `124`. but wait!! didn't `a - a` just give us `3`?

well, yes, and the reason is that each of `a` and `a` are the same as `&a` and `&a` respectively. both `a` and `a` are 2-element `int` arrays (`int`). following the logic above, the size of `int` is 2 `int`s × 4 bytes = 8 bytes.

the expression above evaluated to `3` because `3` units × 8 bytes = 24 bytes.

similarly, `a` and `a` are the same as `&a` and `&a`. the different, in bytes, is still 24. however, the type of `a` and `a` is just `int`, so the size of a unit is 4 and that explains why `a - a` gives `6` (6 units × 4 bytes = 24 bytes).

the last expression, `a - a` doesn't involve pointer arithmetic and is fairly obvious!

This's a normal case of a WTF pointer logic.

In an Array `int x` for example the `x` is a pointer to the first element in the array, as for an array `y` the pointer `y` is pointing towards the first entry in the array `y` which are a pointer to the first element of `y`.

Brackets are simplified version of pointer dereference operator `*` therefore the operation `x` is the same as `*x` and BEHOLD ... the `x` is the same as `*(x+1)`.

In pointer arithmetics if you added `1` to a pointer you actually add the size of the type of the pointer.

In your case the problem is not the multidimensional array, you are subtracting pointers from one another which will give you the distance between them in `data type` metrics.

`a - a = 3` `a` and 'a' are arrays of type `int ` and the distance between the are the three arrays `{1,2},{3,4},{5,6}`.

`a - a = 6` `a` and 'a' are arrays of type `int` and the distance between the are the six integers `{1,2},{3,4},{5,6}`.

Hope I've got it right :)