Errors when initializing arrays of structures for my menu system

I'm working on creating a menu system for an embedded PIC microcontroller and I just need to know if the idea I'm working with is feasible and if so, how I can get the darned thing to compile.

The menu hierarchy is as follows: TOP MENU -> SUB MENU -> ACTIONS

I figured I could create a structure for each of those menu types:

 //Each action contains a name and a process ID to reference when selected
typedef struct{
    char name[20];
    uint8_t proc_id;
} MenuItem;

//Each Sub Menu contains a name and array of actions
typedef struct{
    char name[20];
    MenuItem actions[3];
} SubMenu;

//the top menu contains a name and array of sub menus
typedef struct{
    char name[20];
    SubMenu sub_menus[3];
} TopMenu;

This hierarchy will be static. A Top menu will not contain any actions and sub menus will only contain actions. The goal here is that when I press up/down on a keypad I can simply increment/decrement an index in the menu array.

I now want to assemble all the menus:

const MenuItem OSDSelection0 = {"OSD ACTION 1",0x01};
const MenuItem OSDSelection1 = {"OSD ACTION 2",0x02};
const MenuItem OSDSelection2 = {"OSD ACTION 3",0x03};
MenuItem OSDSelections[3];

const MenuItem LEDSelection0 = {"LED ACTION 1",0x04};
const MenuItem LEDSelection1 = {"LED ACTION 2",0x05};
const MenuItem LEDSelection2 = {"LED ACTION 3",0x06};
MenuItem LEDSelections[3];


const MenuItem FANSelection0 = {"FAN ACTION 1",0x07};
const MenuItem FANSelection1 = {"FAN ACTION 2",0x08};
const MenuItem FANSelection2 = {"FAN ACTION 3",0x09};
MenuItem FANSelections[3];

/*Populate these 3 in init_menu function*/
SubMenu OSDMenu;
SubMenu LEDMenu;
SubMenu FANMenu;
SubMenu SubMenus[3];

TopMenu MainMenu;

//SNIP

void InitMenu(void){
    OSDSelections[0] = OSDSelection0;
    OSDSelections[1] = OSDSelection1;
    OSDSelections[2] = OSDSelection2;

    LEDSelections[0] = LEDSelection0;
    LEDSelections[2] = LEDSelection1;
    LEDSelections[3] = LEDSelection2;

    FANSelections[0] = FANSelection0;
    FANSelections[2] = FANSelection1;
    FANSelections[3] = FANSelection2;

    OSDMenu.actions = OSDSelections;
    OSDMenu.name = "OSD MENU";

    LEDMenu.actions = LEDSelections;
    LEDMenu.name = "LED MENU";

    FANMenu.actions = FANSelections;
    FANMenu.name = "FAN MENU";

    SubMenus[0] = OSDMenu;
    SubMenus[1] = LEDMenu;
    SubMenus[2] = FANMenu;

    MainMenu.name = "MAIN MENU";
    MainMenu.sub_menus = SubMenus;
}

This does not compile. I get lots of errors thrown in my direction about incompatible types (casting char * to char[20], struct MenuItem * to struct MenuItem[3]). I'm starting to think my approach to this could be no good. In general is this solution viable? If so how can I connect this all up properly?

Thanks in advance for any advice.

Answers


Your first error:

error: incompatible types when assigning to type ‘struct MenuItem[3]’ from type ‘struct MenuItem *’

Comes from this line:

OSDMenu.actions = OSDSelections;

The reason for this is because actions is an array of 3 MenuItem structures. OSDSelections is an array of 3 MenuItems, but you can't assign one array to another like this, That's what the compiler is trying to tell you, you're treating actions is if it was a MenuItem *.

Instead you need to do each one individually:

OSDMenu.actions[0] = OSDSelections[0];
OSDMenu.actions[1] = OSDSelections[1];
OSDMenu.actions[2] = OSDSelections[2];

This is the same problem with LEDMenu.actions = LEDSelections; and FANMenu.actions = FANSelections;The other issue you're facing is:

error: incompatible types when assigning to type ‘char[20]’ from type ‘char *’

And that's because of these lines:

OSDMenu.name = "OSD MENU";

OSDMenu.name is an array of 20 characters. You initialized it empty here:

SubMenu OSDMenu;

When you did that, you missed the change to just "assign" a string like this. Now you're trying to take a pointer to the string literal "OSD MENU" and assign it to an array of 20 characters and that doesn't work. That's what the compiler is telling you. You need to do a strcpy() at this point to make the assignment.

strcpy(OSDMenu.name, "OSD MENU");

The same problem occurs with LEDMenu.name = "LED MENU"; and FANMenu.name = "FAN MENU";, and MainMenu.name = "MAIN MENU";


Need Your Help

Child with position:fixed scrolls with position:fixed, overflow:auto parent

css css3 css-transitions css-animations css-transforms

I have a parent with position:fixed; width/height:100%; overflow:auto and a child that also has position:fixed. When the parent's content overflows and the parent starts scrolling I would expect the

Dojo JsonRest Basic Authentication

javascript rest dojo basic-authentication

I want to read JSON Data from a REST-Service with the Dojo JsonRest. The REST-Service requires Username and Password as Basis Authentication String. For the beginning, I hardcoded this string. Now, I