Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I just build my fist LED cube and want to expand the test code a bit. To address each LED of my 3x3x3 cube I want to use a corresponding three-dimensional array, but I got errors on its 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.

share|improve this question
3  
I have to say, your initialization looks more like [3][3][3][1] –  spender Jul 19 '10 at 10:27
1  
Check out chiphacker.com for arduino related questions, more likely to get help over there! –  theorise Jul 19 '10 at 10:38

4 Answers 4

up vote 3 down vote accepted

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
share|improve this answer
    
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 Jul 19 '10 at 11:10

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

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} }
    }
};
share|improve this answer
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} }
}
};
share|improve this answer

(More a tip)

It's seems faster/lighter to boolean instead of ints if you're going to use 0 and 1. I currently working on a big matrix and the arduino

ps: Seems like I can't reply

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.