c - C 结构体中灵活的结构体数组

标签 c

正如标题中所写,我可能对结构中的灵活数组有疑问。 我只是想将指针从常量菜单移动到另一个菜单。我想从当前菜单中提取一些数据并将其发送出去:

    static void DoMainMenuAction(void);

    /* Type */
    typedef enum _LCD_ItemType
    {
        E_LCD_ITEM_TYPE_BOOL,
        E_LCD_ITEM_TYPE_EXPANDABLE,
        E_LCD_ITEM_TYPE_ACTION      
    }e_LCD_ItemType;

    /* Menu Item */
    typedef struct _LCD_MenuItem
    {
        char*           Name;
        e_LCD_ItemType  Type;
    }s_LCD_MenuItem;

    /* Menu structure */
    typedef struct _NAV_Menu
    {
        const char*             Header;     /* Title */
        const uint8_t           NbItems;    /* Number of choices in the menu */
        void (*callbackFnct)(void);         /* Selected item function */
        const s_LCD_MenuItem*   Items[];    /* Menu array of Items */
    } s_NAV_Menu; 

    /* Main Menu */
    static const s_LCD_MenuItem NAV_MainMenuItems[] = {
        { "<< Back",            E_LCD_ITEM_TYPE_ACTION      },
        { "General Settings",   E_LCD_ITEM_TYPE_EXPANDABLE  },
        { "About",              E_LCD_ITEM_TYPE_ACTION      }
    };
    static const s_NAV_Menu NAV_MainMenu = {
       "Menu",
        3,
        &DoMainMenuAction,
        (s_LCD_MenuItem*)&NAV_MainMenuItems
    };

    /* Pointer of current menu */
    static s_NAV_Menu* pCurrentMenu;

    static void NAV_LcdDisplay()
    {    
        uint8_t indexItem = 0;

        /* Move to the Main Menu */             
        pCurrentMenu = (s_NAV_Menu*)&NAV_MainMenu;

        s_LCD_MenuItem NAV_LCD_MenuItems[pCurrentMenu->NbItems];

        for ( indexItem = 0; indexItem < pCurrentMenu->NbItems; indexItem++ )
        {
            NAV_LCD_MenuItems[indexItem].Name = pCurrentMenu->Items[indexItem]->Name;
            NAV_LCD_MenuItems[indexItem].Type = pCurrentMenu->Items[indexItem]->Type;       
        }

        LCD_Send(NAV_LCD_MenuItems);
    }

我的问题出现在 FOR 循环的第二次迭代期间:

第一个循环,我得到: 1st iteration

在第二次迭代中,当我尝试读取项目名称时,我跳转到虚拟处理程序。

有人可以帮忙吗?

最佳答案

/* Menu structure */
typedef struct _NAV_Menu
{
    const char*             Header;     /* Title */
    const uint8_t           NbItems;    /* Number of choices in the menu */
    void (*callbackFnct)(void);         /* Selected item function */
    const s_LCD_MenuItem*   Items[];    /* Menu array of Items */
} s_NAV_Menu; 

Itemss_LCD_MenuItem 类型的指针数组。

也许您想要指向 s_LCD_MenuItem 类型数组的指针。

typedef struct _NAV_Menu
{
    const char*             Header;     /* Title */
    const uint8_t           NbItems;    /* Number of choices in the menu */
    void (*callbackFnct)(void);         /* Selected item function */
    const s_LCD_MenuItem*   Items;    /* Menu array of Items */
} s_NAV_Menu; 

关于c - C 结构体中灵活的结构体数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59282432/

相关文章:

c - 从 UTF8 读取文件

c - 如何在c中获取没有 `scanf()`的整数和浮点输入?

c - 为不同数据类型分配的内存是否取决于体系结构?

c - 计算机如何解释编程语言?

c - Unsigned int 从 32 位到 64 位操作系统

c++ - 如何在cpp和c中不使用cout/printf输出文本?

c - gcc gc-sections 选项删除 crtbegin/crtend 对象

c++ - 音频操作和删除音频的某些部分

c - (整数)4294967295.0 = 2147483647?

c - 什么是文件描述符?