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++) {}
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]}
};