How do I initialize a four dimension array?

I 've been trying to do it but I just can't figure it out. Is it even possible to do such thing?
Thanks.

Yes, you can.

byte testArray[4][4] = {
  1,2,3,4,
  5,6,7,8,
  9,10,11,12,
  13,14,15,16
};

void setup() {
  Serial.begin(115200);

  for(int i = 0; i < 4; i++) {
    for(byte x = 0; x < 4; x++)
      Serial.println(testArray[i][x]);
  }
}

void loop() {
}

That's a 2 dimensional array. While your code happens to work, this is more logical:

byte testArray[4][4] = {
  {1,2,3,4},
  {5,6,7,8},
  {9,10,11,12},
  {13,14,15,16}
};

A four dimensional array might be

byte a[2][2][2][2] = 
{{{{0, 1},
   {2, 3}},
  {{4, 5},
   {6, 7}}},
 {{{8,9},
   {10, 11}},
  {{12, 13},
   {14, 15}}}} ;

Thank you MarkT. You are correct. Here is a 4 dimensional array.

byte testArray[2][2][2][2] = {
  1,2,
  3,4,
  5,6,
  7,8,
  9,10,
  11,12,
  13,14,
  15,16
};

void setup() {
  Serial.begin(115200);

  for(byte i = 0; i < 2; i++) {
    for(byte x = 0; x < 2; x++) {
      for(byte y = 0; y < 2; y++) {
        for(byte z = 0; z < 2; z++)
          Serial.println(testArray[i][x][y][z]);
      }
    }
  }
}

void loop() {
}

I can see that the full version with braces is actually a bit of a mess with lots of dimensions :slight_smile:

You don't need the inner braces for a multi-dimensional array, they would only be a convenience to group the different dimensions together. You can initialize any dimensioned array using a linear list. The general rule is that it starts at index all-0, and it is incremented at the right-most index until all places are filled or the initializers run out.

Thank you very much, to the both of you! But I'm getting dizzy with the braces thing.
I need to initialize this array: byte a[2][8][4][3];
Could you help me again?

Surely whatever you're trying to do could be handled with something more simple than a four dimensional array?

Maybe you could describe what you're trying to achieve so the smart people around here could provide a better alternative.

Arqfas:
Thank you very much, to the both of you! But I'm getting dizzy with the braces thing.
I need to initialize this array: byte a[2][8][4][3];
Could you help me again?

Just lay out your code logically.

byte a[2][8][4][3] = 
{
  {
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}}
  },
  {
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}},
    {{10,11,12}, {13,14,15}, {16,17,18}, {19,20,21}}
  }
};

Lovely!