0
struct Menu
{
    float id;
    char item[50];
    struct Menu* subMenu[10];
} Menu[5] = {
    {1, "SEARCH YOUR CONTACT", (struct Menu[]){{1.1, "ADD TO FAVOURITES"}, {1.2, "UPDATE"}, {1.3, "DELETE"}, {1.4, "ADD FIELD"}, {1.5, "BACK TO MAIN MENU"}}},
    {2, "ADD CONTACT"},
    {3, "DISPLAY FAVOURITES CONTACT", (struct Menu[]){{3.1, "ADD TO FAVOURITES"}, {3.2, "UPDATE"}, {3.3, "DELETE"}, {3.4, "ADD FIELD"}, {3.5, "BACK TO MAIN MENU"}}},
    {4, "DISPLAY ALL CONTACT", (struct Menu[]){{4.1, "ADD TO FAVOURITES"}, {4.2, "UPDATE"}, {4.3, "DELETE"}, {4.4, "ADD FIELD"}, {4.5, "BACK TO MAIN MENU"}}},
    {5, "EXIT APPLICATION"}
};
void menuItem()
{
    for (int i = 0; i < 5; i++)
    {
        printf(" ... %.1f \\", Menu[0].subMenu[i]->id);
    }
}

Output:

... 1.1 \Error: segmention fault

I tried printing method

printf("%.1f", Menu[0].subMenu[1]->id);

but it didn't work.

I want to print out all elements of the array struct.

8
  • 1
    Your 2nd element ("ADD CONTACT") does not have any subMenu entries. Commented Jan 12, 2023 at 7:44
  • 4
    You declare subMenu as an array of pointers, but you initialize it with an array of structure objects. Commented Jan 12, 2023 at 7:45
  • 3
    On an unrelated note: Using a float as the id is probably not a good idea. What happens if you use an id that can't be represented exactly as a float? And how would you use sub-sub-menus if you can't have e.g. 1.1.1? Commented Jan 12, 2023 at 7:51
  • 2
    Extending on @Someprogrammerdude : All of your ids x.y apart from x.5 cannot be represented exactly as they are periodic in binary... Commented Jan 12, 2023 at 7:58
  • 1
    I find the data, at least as written, impossible to read. You already got feedback on the id. Fixed size item is odd, as is fixed size sub-menus. Strings use a '\0' sentinel, you can use a special id, or have it hold and array of pointers to menus and use NULL to signify end of list. I would probably model this as a flat array of { unsigned char level, char *item} and let the position in the array be id. Commented Jan 12, 2023 at 8:07

1 Answer 1

2

Others already pointed out the problem with float id so I suggest you use a menu and submenu indices instead (either as is, a struct, or as @Fe2O3 pointed out mapped into an integer like unsigned id = m << 8 | sm). I made item a char * instead of fixed size and the sub-menu an array of pointers to Menu:

#include <stdio.h>

struct Menu {
    char *item;
    struct Menu **subMenu;
} Menu[] = {
    {"SEARCH YOUR CONTACT", (struct Menu *[]) {
        &(struct Menu) {"ADD TO FAVORITES"},
        &(struct Menu) {"UPDATE"},
        &(struct Menu) {"DELETE"},
        &(struct Menu) {"ADD FIELD"},
        &(struct Menu) {"BACK TO MAIN MENU"},
        NULL
    }},
    {"ADD CONTACT", NULL},
};

int main() {
    for (int m = 0; m < sizeof Menu / sizeof *Menu; m++) {
        printf("item: %s\n", Menu[m].item);
        for(int sm = 0;  Menu[m].subMenu && Menu[m].subMenu[sm]; sm++) {
            printf("  item: %s\n", Menu[m].subMenu[sm]->item);
        }
    }
}

Here is the example output:

item: SEARCH YOUR CONTACT
  item: ADD TO FAVORITES
  item: UPDATE
  item: DELETE
  item: ADD FIELD
  item: BACK TO MAIN MENU
item: ADD CONTACT

Alternatively use a flat array and an unsigned char or enum to indicate the level:

#include <stdio.h>

struct Menu {
    unsigned char level;
    char *item;
} Menu[] = {
    {0, "SEARCH YOUR CONTACT"},
    {1, "ADD TO FAVORITES"},
    {1, "UPDATE"},
    {1, "DELETE"},
    {1, "ADD FIELD"},
    {1, "BACK TO MAIN MENU"},
    {0, "ADD CONTACT"},
};

int main() {
    for(int m = 0; m < sizeof Menu / sizeof *Menu; m++) {
        printf("%*s%sitem: %s\n", 2 * Menu[m].level, "", Menu[m].item);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.