Two dimension callback array

Hi,

I made a menu system for my project. I have some callbacks in threes arrays

void (*menu0_callbacks)(void) = { go_to_menu_1, go_to_menu_2, change_a};
void (*menu1_callbacks)(void) = { do_a, do_b, do_c, go_to_menu_0};
void (*menu2_callbacks)(void) = { do_e, do_f, go_to_menu_0};

It is working fine, but before each call of a callback, I have to test the actual menu number to choose the corresponding callbacks menu :

if (menu == 0) menu0_callbacksitem_selected;
else if (menu == 1 ) menu1_callbacksitem_selected;
else if (menu == 2 ) menu2_callbacksitem_selected;

How can I change that to just have to use ?

menu_callbacks[menu]item_selected;

I have to create a two dimension array of function pointer, but it is not straightforward !

Thank you

zarg404:
How can I change that to just have to use ?

menu_callbacks[menu][item_selected]();
void (*menu_callbacks[4][3])(void) = 
{
{ go_to_menu_1, go_to_menu_2, change_a,     NULL},
{ do_a,         do_b,         do_c,         go_to_menu_0},
{ do_e,         do_f,         go_to_menu_0, NULL}
};

John - are the array level numbers in the declaration the right way round ?

I always think of the 2 dim array specifiers as rows and columns but that array seems to have 3 rows of 4 values not 4 rows of 3 values

SOME_TYPE myArray[numRows][numCols];

Thank you !

I think it should be

void (*menu_callbacks[3][4])(void) =
{
{ go_to_menu_1, go_to_menu_2, change_a,     NULL},
{ do_a,         do_b,         do_c,         go_to_menu_0},
{ do_e,         do_f,         go_to_menu_0, NULL},
{ do_h, NULL}
};

UKHeliBob:
John - are the array level numbers in the declaration the right way round ?

Probably not. I am ALWAYS confused about which dimension does where AND which one can be left empty. :frowning:

zarg404:
I think it should be

void (*menu_callbacks[3][4])(void) =

{
{ go_to_menu_1, go_to_menu_2, change_a,     NULL},
{ do_a,         do_b,         do_c,         go_to_menu_0},
{ do_e,         do_f,         go_to_menu_0, NULL},
{ do_h, NULL}
};

Your example didn't have a 'menu_3' or a 'do_h()' but, OK:

void do_a(), do_b(), do_c(), do_d(), do_e(), do_f(), do_g(), do_h();
void go_to_menu_0(), go_to_menu_1(), go_to_menu_2();
void change_a();

void (*menu_callbacks[][4])(void) =
{
{ go_to_menu_1, go_to_menu_2, change_a},
{ do_a,         do_b,         do_c,         go_to_menu_0},
{ do_e,         do_f,         go_to_menu_0},
{ do_h}
};

I am ALWAYS confused about which dimension does where AND which one can be left empty.

For some reason I can remember [rows][columns] but I agree about remembering which can be omitted. If in doubt leave them all out and let the compiler remind you

My bete noir is the order of the pins for Softserial for which the compiler is no help at all. If I had written the library I would have used txPin, rxPin but it is the other way round, or is it ? I always have to check