You've specified an array with three dimensions, but only specified two. Furthermore, each element is supposed to have two entries, but you've got three.
If you turn on warnings you should see warnings like these:
test.c:4:26: warning: suggest braces around initialization of subobject [-Wmissing-braces]
int mat[2][2][2] = {{3,1,5},{2,2,2}};
...more like that...
test.c:6:44: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
printf("z for first coordinate = %d\n",mat[0][2]);
~~ ^~~~~~~~~
test.c:6:44: warning: array index 2 is past the end of the array (which contains 2 elements)
[-Warray-bounds]
printf("z for first coordinate = %d\n",mat[0][2]);
^ ~
test.c:4:5: note: array 'mat' declared here
int mat[2][2][2] = {{3,1,5},{2,2,2}};
^
int mat[2][2][2]
would initialize like this.
int mat[2][2][2] = {
{
{3,1}, {1,1},
},
{
{1,1}, {2,2},
}
};
You'll never get 3 2 2
from mat[0][1][1]
. It will only ever return a single value. In this case, 1.
If you wish to store a list of 2 3D coordinates, use [2][3] instead.
int mat[2][3] = {{3,1,1},{2,2,2}};
Asking what z is for the first x and second y doesn't make sense. It's like asking what the altitude of Denver, New Jersey is (Denver is in Colorado). First x and second y are part of two different coordinates and have different z's.
Instead, you can get the z for the first coordinate like so.
printf("z for first coordinate = %d\n",mat[0][2]);
The first coordinate is mat[0]
and its z is the 3rd attribute which is the 2nd index. mat[0][2]
.