xextrude:
String menu1[] = {"LED","Serial Read","Infrared Read"};
String menu2[] = {"ON" , "OFF"};
String menu3[] = {"Show in BIN","Show in HEX"};
Let's assume you are using char arrays instead of strings.
There are two main ways to do this - a two dimensional array, and a "ragged" array.
A two dimensional array crates a block of - in this case - nine pointers to char, and initializes those pointers to point at character constants
char * menu[3][3] = {
{"LED","Serial Read","Infrared Read"},
{"ON" , "OFF"},
{"Show in BIN","Show in HEX"}
};
[code]
There is a bit of a problem here, because one of your rows only has two items in it. There are a couple of ways to fix that. One is to store the count of items in each row in another array, or to use a struct.
But the usual way is to use a marker at the end of the list - something that's not a string. Specifically, you use NULL. The benefit of this is that in if and while statements, NULL evaluates to boolean false, meaning that you can use it as a loop condition.
[code]
char * menu[3][4] = {
{"LED","Serial Read","Infrared Read", NULL},
{"ON" , "OFF", NULL},
{"Show in BIN","Show in HEX", NULL}
};
…
for(int i = 0; i < 3; i++) {
Serial.println("-----")
for(j = 0; j<3;j++) {
if(!menu[i][j]) break;
Serial.println(menu[i][j]);
}
}
The other way to do this is with a ragged array. In this, rather than storing a 3*4 array, you store an array of pointers to arrayts of ponters to strings (arrays of char). These can each be any length.
char * m1[] = {"LED","Serial Read","Infrared Read",NULL};
char *m2[] = {"ON" , "OFF",NULL};
char *m3[] = {"Show in BIN","Show in HEX",NULL};
char **menu[] = {m1, m2, m3, NULL};
You can address these exactly the same as with the code above, using i and j. However, the use of ragged arrays allows you to use pointers to step through the arrays.
for(char ***m = menu; *m; m++) {
for(char **i = *m; *i; i++) {
Serial.print(*i);
}
}