Problem with arrays

Hi everyone!

I am trying to build up a menu-structure like this:

| | submode = 0 | submode = 1 | submode = 2 | | - | - | - | - | | mode = 0 | Open | Save | | | mode = 1 | Cut | Copy | Paste | | ... | ... | ... | ... |

I have two possibilities in mind, how to solve this: - Two-dimensional array and a - One-dimensional array

The problem is, that as strings are arrays of chars, the 2-dimensional would actually be a 3-dimensional, right? Would this be possible in Arduino?

Here my theoretical ideas: Two-Dimensional solution

Would be something like

menuText[0][0] = "Open";
menuText[0][1] = "Save";
// etc.

How would this example look like in Arduino-style?

One-Dimensional solution The other idea (let´s suggest, that there are not more than 5 submodes):

// Mode 0: submode 0 to submode 1
menuText[0*5+0] = "Open";
menuText[0*5+1] = "Save";

// Mode 1: submode 0 to submode 2
menuText[1*5+0] = "Cut";
menuText[1*5+1] = "Copy";
menuText[1*5+2] = "Copy";

// etc.

What about memory usage? Would the second example use more or less memory? How would this look like in Arduino?

Thanks a lot! Sebastian

You can create an array of character strings like this: char * menuText[] = {"Open", "Save", "Close"};

and use them like this: Serial.print(menuText[1]); // print "Save" (the second element in the array)

Ok, cool! And how would a 2-dimensional array look like?

C multidimensional arrays are declared as follows char * menuText[ROWS][COLUMNS] ;

this would be initialized in row order, that is you initialize all of the column strings for row 0, then row 1 etc. Your main menu could be row zero and the other rows would be your submenus.

But I don't advise that approach for menu strings because memory will wasted for any submenu that has less items the biggest menu. It can also be confusing to code.

I suggest you define an array for each menu or submenu.

Menus can be tricky to code, start of with something simple to get the hang of it.

In case it might be useful:

A framework for creating menus. :)

That looks very useful. I would think it would save a lot of time compared to building a menu from scratch.