increment Array Name with int

Bonjour,

I’d like to increment array’s name with an Integer and display the elements of the selected array

For exemple:
I 've got the following arrays :

String tab0[5]={"moi",toi","nous",vous","eux","why"};
String tab1[3]={"moi",toi","vous"};
String tab2[3]={"abc",def","ghi","jkl"};



int a;
char montableau[4]

I tried

sprintf(montableau,"tab%d",a);

for (a=0;a<3;a++)
 {Serial.print(montableau);  // I got the results tab0 tab1 tab2
   Serial.print(montableau[0]);  // montableau is a char array. I'd like 'Moi'

it displays only one char, for exemple 'm' from moi for tab0[0] but sprintf doesn't take String montableau[4];
 }

Could you help me please to increment my variable and display all the string and not only one char

Thanks.

String montableau[];

int a;
char montableau[4];

Does that really compile without errors?

Post complete code!

String montableau[]; What use is an array with zero elements?

First, there are some syntax errors (e.g., missing quotes around toi), plus I’m not a big fan of the String class since it has some known errors. Try this code as a start:

void setup() 
{
  Serial.begin(9600);
  while (!Serial) // Waste time until Serial is created
    ;

  int index;
  char  *tab0[] = {"moi", "toi", "nous", "vous", "eux", "why"};

  for (index = 0; index < sizeof(tab0) / sizeof(tab0[0]); index++) {
    Serial.println(tab0[index]);
  }
}

void loop() {
}

You can use the Right-Left Rule to figure out that tab[] is an array of pointers to char.

You cannot create variable names on the fly at runtime. At runtime there are NO variable names. They are all converted to memory addresses during compilation and linking.

Of course, this is something I tried but I got problem with the sprintf so I used char instead of string. I deleted String montableau from my previous post.

econjack, thanks for your help but I need to increment your tab0 with a byte(or integer) , such as tab0,tab1, tab2

something like “tab%d”,a with 0<a<3 for example.

Thanks

booby75:
econjack, thanks for your help but I need to increment your tab0 with a byte(or integer) , such as tab0,tab1, tab2

something like “tab%d”,a with 0<a<3 for example.

Thanks

Or you could use subscripts, such as tab[0], tab[1], tab[2] for example.

String* tab[] = { &tab0, &tab1, &tab2 };

Edit: Correction, as eco jack points out I missed out the .

As Ray pointed out, you cannot increment array names…they don’t exist at runtime. Any variable has a fixed address (i.e., an lvalue) that is used to reference that variable.

@pcbbc: should tab be an array?

Finally I use instead :

switch (a) {
       case 1:
Serial.print(tab1[random(0,9)]); 

           break;

   
       case 2:
Serial.print(tab2[random(0,9)]); 

           break;

....

Thanks

Why 'random(0,9)' when none of your arrays have more than 6 elements?

You can structure an array as two-dimensional ("2D") and access 'pages'.

https://forum.arduino.cc/index.php?topic=44228.0

Maybe that helps?

Any array of char** works nicely:

const char * const tab0[] = {"moi", "toi", "nous", "vous", "eux", "why", nullptr};
const char * const tab1[] = {"moi", "toi", "vous", nullptr};
const char * const tab2[] = {"abc", "def", "ghi", "jkl", nullptr};

const char * const *allTabs[] = {tab0, tab1, tab2};
constexpr size_t numTabls = sizeof(allTabs) / sizeof(allTabs[0]);

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

  for (size_t i = 0; i < numTabls; i++) {
    const char * const *ptr = allTabs[i];
    size_t j = 0;
    while (1) {
      Serial.print(ptr[j]);
      j++;
      if (ptr[j]) {
        Serial.print(", ");
      } else {
        Serial.println();
        break;
      }
    }
  }

}

void loop() {
}

Serial Monitor Output:

moi, toi, nous, vous, eux, why
moi, toi, vous
abc, def, ghi, jkl