views:

93

answers:

2

I just build my fist LED-Cube an want to expand the test code a bit. To address each LED of my 3x3x3-cube I want to use a corresponding 3-dimensional array but I got errors on it's initialization.

Here's what I did:

int cube_matrix[3][3][3] = 
{
    { {0}, {0}, {0} },
    { {0}, {0}, {0} },
    { {0}, {0}, {0} }
},
{
    { {0}, {0}, {0} },
    { {0}, {0}, {0} },
    { {0}, {0}, {0} }
},
{
    { {0}, {0}, {0} },
    { {0}, {0}, {0} },
    { {0}, {0}, {0} }
};

Here's the error I get:

error: expected unqualified-id before '{' token

I could use a for-loop to initialize my array and get things done but my initialization seems correct to me and I want to know what I did wrong.

Thx in advance.

A: 

If you're really aiming for allocating the whole thing with zeros, you could use a simplified initializer:

int cube_matrix[3][3][3] = {0};

If you'd like more than zeros in there, you can do that too:

#include <stdio.h>

int main(int argc, char* argv[]) {

    int cube_matrix[3][3][3] = {1, 2, 3, 4, 5};
    int i, j, k;

    for (i=0; i<3; i++)
            for (j=0; j<3; j++)
                    for (k=0; k<3; k++)
                            printf("%i %i %i: %i\n", i, j, k, cube_matrix[i][j][k]);

    return 0;
}

With output that looks like this:

$ ./a.out
0 0 0: 1
0 0 1: 2
0 0 2: 3
0 1 0: 4
0 1 1: 5
0 1 2: 0
0 2 0: 0
0 2 1: 0
0 2 2: 0
1 0 0: 0
1 0 1: 0
1 0 2: 0
1 1 0: 0
1 1 1: 0
1 1 2: 0
1 2 0: 0
1 2 1: 0
1 2 2: 0
2 0 0: 0
2 0 1: 0
2 0 2: 0
2 1 0: 0
2 1 1: 0
2 1 2: 0
2 2 0: 0
2 2 1: 0
2 2 2: 0
sarnold
For this purpose I like the way with many curly braces better to visualize my cube in the code. But I never knew about initialization ob multi-dim-arrays with just one pair of braces.Thx.
mantuko
+2  A: 

You need an extra set of curly braces around your array element. You are missing the outer set.

leppie