3
\$\begingroup\$

I'm using MPLAB X IDE with XC8 compiler and I would like to create a menu system to a GLCD module, but I'm having trouble initializing my MainMenuItemsList[] array. I'm not getting error on compliling but if I'm debugging the project, I can see that only the first item is initialized. What can be the cause?

Here is the relevant code fragment:

typedef struct menuitem
{
  char ItemName[10];
  void (*handler)(void);
  struct menu *ChildSubMenu;
}MenuItem;

typedef struct menu
{
  char MenuName[10];
  char NumberItems;
  //struct menu *ParentMenu;
  MenuItem *Items[12];
}Menu;

MenuItem MainMenuItemsList[12] = {  {"SubMenu1", NULL, NULL},
                                    {"SubMenu2", NULL, NULL},
                                    {"SubMenu3", NULL, NULL},
                                    {"SubMenu4", NULL, NULL},
                                    {"SubMenu5", NULL, NULL},
                                    {"SubMenu6", NULL, NULL},
                                    {"SubMenu7", NULL, NULL},
                                    {"SubMenu8", NULL, NULL},
                                    {"SubMenu9", NULL, NULL},
                                    {"SubMenu10", NULL, NULL},
                                    {"SubMenu11", NULL, NULL},
                                    {"SubMenu11", NULL, NULL}
                                };

Menu MainMenu = {"MainMenu",12,&MainMenuItemsList};
\$\endgroup\$
  • \$\begingroup\$ How do you know that the array is not being initialized correctly? Did you tested on an actual LCD? Some of the later versions of MPLAB X are not displaying the correct values on the watch window. \$\endgroup\$ – Bruno Ferreira Nov 16 '14 at 1:18
  • \$\begingroup\$ Yes I've tested it. The strings "MainMenu" and "SubMenu1" are written correctly on the LCD but the other arrays ItemName[10] are full of null characters instead of SubMenu2...11. (By the way I'm using MPLAB X v2.15) \$\endgroup\$ – John Nov 16 '14 at 1:40
  • \$\begingroup\$ It wont function because I need a list of structs, from that I select the first member and write the name of it (SubMenu1) to the screen than I choose the second element etc. That appears in my code like this: void drawMenu(Menu *menuToShow, unsigned char selectedIndex) { ... glcd_puts(menuToShow->Items[i]->ItemName,8,i+1,0,1,-2); ... } And the call of the function: drawMenu(&MainMenu, 0); Sorry but I don't know how to format the code block properly, but I tried it for several times. \$\endgroup\$ – John Nov 16 '14 at 13:48
  • \$\begingroup\$ Does nobody have an idea? I've tried this too, but it didn't worked. link \$\endgroup\$ – John Nov 20 '14 at 18:45
2
\$\begingroup\$

when you define MenuItem *Items[12]; you are creating an array of addresses, not an address to an array of structs (which is what you want).

take the "[12]" out:

typedef struct menu
{
  char MenuName[10];
  char NumberItems;
  MenuItem * Items;
}Menu;
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.