开发者

How to initialize three-dimensional array in Arduino?

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.


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} }
    }
};


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


You don't need to use all that braces, try this:

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 }
  }
};


(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


Try something like:

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} }
}
};


int cube_matrix[3][3][3] = {

{{0,1,2},// row 0
{3,4,5},
{6,7,8}},


 {{9,10,11},
  {12,13,14},
  {15,16,17}},

 {{18,19,20},
  {21,22,23},
  {24,25,26}}


};



void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
for (int i=0;i<3;i++){
  for (int j=0;j<3;j++){
    for(int k=0;k<3;k++){

  Serial.println(cube_matrix[i][j][k]);
  delay(1000);
   }
  }
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜