简体   繁体   English

为什么我不能打印出结构数组

[英]Why can't I print out array of struct


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 \ output: ... 1.1 \

Error: segmention fault错误:段错误

I tried printing method printf("%.1f", Menu[0].subMenu[1]->id);我试过打印方法 printf("%.1f", Menu[0].subMenu[1]->id); but it didn't work,但它没有用,

I want to print out of all elements in the array of struct.我想打印出结构数组中的所有元素。

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 ).其他人已经指出了float id的问题,所以我建议您改用菜单和子菜单索引(按原样,结构,或者 @Fe2O3 指出映射到 integer,如unsigned id = m << 8 | sm )。 I made item a char * instead of fixed size and the sub-menu an array of pointers to Menu:我将项目设为char *而不是固定大小,并将子菜单设为指向菜单的指针数组:

#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:这是示例 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:或者使用平面数组和unsigned char或枚举来指示级别:

#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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能使用以前在课堂上制作的数组来打印电话簿的号码? - Why can't I use the array I made in a class earlier to print out the numbers for a phone book? 为什么我不能打印出我的字符串数组c ++? - Why can't I print out my string array c++? 为什么我不能打印2D阵列? - Why can't I print my 2D array? 为什么我不能打印带有 range() 的随机 integer numpy 数组 - Why can't I print a random integer numpy array with range() 为什么不能打印存储在创建的数组中的值? - Why can't I print the values that are stored in the created array? MIPS为什么我无法打印所选的阵列位置? - MIPS Why I can't print the selected array position? c,为什么我不能打印数组的所有元素 - c, why i can't print all elements of my array 我不知道如何打印数组,反过来,我也不知道如何交换数组中的元素 - I can't figure out how to print arrays and in turn, I can't figure out how to swap elements in an array 我无法访问结构数组上的字段 - I can't access the fields on a struct array 如何从多维数组中仅打印一个元素? (我不知道该数组的布局方式) - How would I print just one element out of this multidimensional array? (I can't tell how this array is laid out)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM