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