[RESOLVED] variable name with a for loop , Is it possible ? see example

Hi i would like to know if this is possible to change variable name with a for loop , and if not have an explanation.

If it is possible could you please learn me the way.

See the examples above for what i want to know:

THIS CODE

byte i;
byte Example1[5] = {1, 2, 3, 4, 5} ;
byte Example2[5] = {11, 12, 13, 14, 15} ;
byte Example3[5] = {31, 32, 33, 34, 35} ;

void setup() {
  Serial.begin(115200);
  Serial.println("HELLO");
  Serial.println("");
}

void loop() {
  for (i = 0; i < 5; i++) {
    Serial.println(Example1[i]);
  }
  for (i = 0; i < 5; i++) {
    Serial.println(Example2[i]);
  }
  for (i = 0; i < 5; i++) {
    Serial.println(Example3[i]);
  }

  Serial.println("END");
  Serial.println("");
  delay(5000);
}

TO THIS CODE

byte i;
byte n;
byte Example1[5] = {1, 2, 3, 4, 5} ;
byte Example2[5] = {11, 12, 13, 14, 15} ;
byte Example3[5] = {31, 32, 33, 34, 35} ;

void setup() {
  Serial.begin(115200);
  Serial.println("HELLO");
  Serial.println("");
}

void loop() {
  for (n = 1; n < 4; n++) {
    for (i = 0; i < 5; i++) {
      Serial.println(Example(n)[i]);   // dont work
      //Serial.println((Example)(n)[i]);  // dont work either
      //Serial.println(Example(n)[i]);  // dont work either
      //Serial.println(Example+n[i]);  // dont work either
                                                 //  and i tried many other ways 
    }
  }

  Serial.println("END");
  Serial.println("");
  delay(5000);
}

Obviously this dont work , but what is the correct way ?

No, it doesn't work that way. Variable names aren't strings. When the code is compiled, the variable names all go away and are replaced by simple memory addresses. So the code when it is running has absolutely no idea what you named your variables.

You obviously understood in the first example you posted that the answer was to put things into an array. The answer to the second example is the same. You now need an array of arrays, a 2 dimensional array.

OK nice , i didnt know the name was 2D array , or didnt even know array can be inside an array .

here is the working code after a fast learning on google

Thank you very much

byte i;
byte n;
byte Exemple[3][5] = { {1, 2, 3, 4, 5}, {11, 12, 13, 14, 15}, {31, 32, 33, 34, 35} } ;

void setup() {
  Serial.begin(115200);
  Serial.println("HELLO");
  Serial.println("");
}

void loop() {
  for (n = 0; n < 3; n++) {
    for (i = 0; i < 5; i++) {
      Serial.print("VALUE : ");
      Serial.println(Exemple[n][i]);
    }
  }

You can also leave the first dimension of the array unspecified and let the compiler figure out how many rows you have:

byte i;
byte n;
byte Example[][5] = { {1, 2, 3, 4, 5}, {11, 12, 13, 14, 15}, {31, 32, 33, 34, 35} } ;
const byte numRows = sizeof(Example) / sizeof(Example[0]);

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("HELLO");
  Serial.println("");
  for (n = 0; n < numRows; n++) {
    for (i = 0; i < 5; i++) {
      Serial.print("VALUE : ");
      Serial.println(Example[n][i]);
    }
  }
}

void loop() {}

You can also leave the first dimension of the array unspecified and let the compiler figure out how many rows you have:

You can also let the compiler figure out how many columns you have.

const byte numCols = sizeof(Example[0]) / sizeof(Example[0][0]);

Then, there is no reason to use magic numbers in the code.