1

Good day folks. I'm new here and have been starting to be enthusiast in Arduino. I have a problem and I don't know what I should do, or whether I did something wrong regarding an array of arrays. Surfed on the internet to find a bunch of examples but the only difference is that they don't use arrays that have been declared to have a name inside array.

Every array inside array I see that works is like this:

byte array[2][4] = { {1,2,3,4}, {5,6,7,8} };
Serial.println(array[0][2]); is equal to '3'.

But mine is like this:

byte arrayX[4] = {1,2,3,4};
byte arrayY[4] = {5,6,7,8};

byte arrayZ[2][4] = { arrayX[4], arrayY[4] };

Serial.println(arrayX[2]); is equals to '3'.
Serial.println(arrayZ[0][2]); is equal to '0'.

Why?

Below is what in reality I am experiencing. I hope someone can try this out too.

softAddr = cycle * 2;

is an expression I used for looping where cycle is the iterated variable

for (byte cycle = 0; cycle < 7; cycle++) {}

enter image description here

I did a simple simulation and I guess the way I initialize the parent array, it initializes a new array in the name same from the declared variables but separate.

byte arrayX[4] = {1,2,3,4};
byte arrayY[4] = {5,6,7,8};

void setup() {
  Serial.begin(9600);
  byte glass[2][4] = {arrayX[4], arrayY[4]};
  Serial.print(F("OLD 1: ")); Serial.println(arrayX[0]);
  Serial.print(F("OLD 2: ")); Serial.println(arrayX[1]);
  Serial.print(F("NEW 1: ")); Serial.println(`glass[0][0]`);
  Serial.print(F("NEW 2: ")); Serial.println(`glass[0][1]`);
}

output:

OLD 1: 1

OLD 2: 2

NEW 1: 0

NEW 2: 0

//answered realized

I forgot that the array I put inside the array already had its data based on its index and the [4] resulted to nothing or 0 since there is no value at index [4] (knowing, indexes where 0, 1, 2, 3 on a 4 sized array). I realized this after I made a simple loop declaration not instantly like this:

byte arrayZ[2][4] = { arrayX[1], arrayY[1] };

But like this:

byte arrayX[4] = {10,11,12,13};
byte arrayY[4] = {20,21,22,23};
byte arrayZ[2][4];
void setup() {
  Serial.begin(9600);
  for (byte parent = 0; parent < 2; parent++) {
    for (byte child = 0; child < 4; child++) {
      if (parent == 0) {
        arrayZ[parent][child] = arrayX[child];
      } else {
        arrayZ[parent][child] = arrayY[child];
      }
    }
  }
  for (byte parent = 0; parent < 2; parent++) {
    for (byte child = 0; child < 4; child++) {
      Serial.print(F("value: ")); Serial.println(arrayZ[parent][child]);
    }
  }
}

Output:

value: 10

value: 11

value: 12

value: 13

value: 20

value: 21

value: 22

value: 23

Resulting in simply like this:

byte arrayZ[2][4] = {
  {arrayX[0],arrayX[1],arrayX[2],arrayX[3]},
  {arrayY[0],arrayY[1],arrayY[2],arrayY[3]}
};
  • done a simple edit on post – Mheruian Jul 31 '17 at 1:43
  • I don't see the point in this? You're just re-naming variables here. Waste of memory. – Jasmine Jul 31 '17 at 18:53
3

Something is wrong with your type description:

byte arrayX[4] = {1,2,3,4}; byte arrayY[4] = {5,6,7,8};

byte arrayZ[2][4] = { arrayX[4], arrayY[4] };

Serial.println(arrayX[2]); is equals to '3'.

This is normal ... arrayX[0] = 1, arrayX[1] = 2, arrayX[2] = 3

However, the next one is not good:

byte arrayZ[2][4] = { arrayX[4], arrayY[4] };

You are declaring a byte array, but you add arrayX[4] and arrayY[4] which, where arrayX[4] is the 5th elements of arrayX but it has only 4 elements, so it points to memory outside the array (probably into the next array.

byte arrayX[4] = { 1, 2, 3, 4 };
byte arrayY[4] = { 5, 6, 7, 8 };

byte* arrayZ[2] = { arrayX, arrayY };

Serial.print(arrayZ[0][2]);

This should print 3 (not tested).

  • just got realized that one also :D thank you for your answer – Mheruian Jul 31 '17 at 2:26
  • You're welcome (please upvote if it helped you) – Michel Keijzers Jul 31 '17 at 2:28
  • yup already did (^_^)b – Mheruian Jul 31 '17 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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