3 Dimensional Arrays and Pointers

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

printf( "%u\n", a);
printf( "%u\n", *a);
printf( "%u\n", **a);
printf( "%d\n", ***a);
printf( "%u\n", a+1);
printf( "%u\n", *a+1);
printf( "%u\n", **a+1);
printf( "%d\n", ***a+1);

Can somebody the explain the output, and why it is so?

(a, *a, **a) Outputs the same address a[0][0][0]
(***a) Outputs the value of a[0][0][0]

(a+1) Outputs the address a[1][0][0]
(*a+1) Outputs the address a[0][1][0]
(**a+1) Outputs the address of a[0][0][1]
(***a+1) ??
***a+1 is (***a) + 1, not ***(a + 1)
The compiler shall isuue an error because one closing brace is absent.:)

1
2
3
int a[2][3][2] = {
 { {2,4}, {7,8}, {3,4} },
 { {2,2}, {2,3}, {3,4} };
As for your question then if you have an array named for example a then *a returns reference to its first element.
]
In your example a is a three-dimensional array. Its element is a two dimensional array. That is a three-dimensional array is an array of two-dimensional arrays.

So *a is the first element of the three dimensional array.

{ {2,4}, {7,8}, {3,4} },

It is in turn an array the element of which is one dimensional array. So **a is the first alement of the two-dimensional array that is {2,4}.

***a will give the first element of this one-dimensional array (**a).
In your example it is 2.
***a + 1 is equivalent to 2 + 1 and will be equal to 3.

printf( "%d\n", ***a+1); will output 3.
Last edited on
Wow, thank you for the clear explanations, was very helpful.
Last edited on
Topic archived. No new replies allowed.