gpt4 book ai didi

c - 声明和定义相互引用的初始化常量结构的最佳方法

转载 作者:行者123 更新时间:2023-11-30 15:11:38 25 4
gpt4 key购买 nike

有很多关于如何定义相互引用的结构的帖子,但我没有看到关于使用这些定义的结构创建的变量的帖子。

我的产品中有许多可以导航的菜单,这些菜单由两种结构表示。此代码构建了我想要的结构:

typedef void (*MENU_FUNCTION_TYPE)();

typedef enum
{
MENU_INPUT_NAV, // Menu just navigates to other menus.
MENU_INPUT_ACTION, // Menu displays the results of an action.
MENU_INPUT_NUM, // Menu take numeric input and processes it.
NUM_MENU_INPUTS
} MENU_INPUT_TYPE;

// A menu is comprised of a list of menu items which you select with the number keys.
// This structure defines a menu and the menu items it contains.
struct MENU_ITEM_TYPE;
struct MENU_TYPE
{
PGM_P text; // Pointer to text to display as menu name.
const MENU_ITEM_TYPE* menu_items; // Pointer to structures describing each menu item in this menu.
uint8_t num_items; // The number of menu item in this menu.
MENU_INPUT_TYPE type; // Specifies how this menu processes keyboard input.
};

// This structure defines a menu item and what it does when you select it.
struct MENU_ITEM_TYPE
{
PGM_P text; // Pointer to text to display as menu item name.
const MENU_TYPE* link; // Pointer to which menu selecting this menu item takes you.
MENU_FUNCTION_TYPE action; // Pointer to function that is executed when this menu item is selected.
};

extern const MENU_TYPE PROGMEM main_menu;
extern const MENU_TYPE PROGMEM todo_menu;
extern const MENU_TYPE PROGMEM todo2_menu;
extern const MENU_TYPE PROGMEM todo3_menu;
extern const MENU_TYPE PROGMEM todo4_menu;

// **************************
// "Main Menu"
const char PROGMEM main_menu_text[] = "Main Menu";
const char PROGMEM main_menu_item_text1[] = "First Menu";
const char PROGMEM main_menu_item_text2[] = "Second Menu";
const char PROGMEM main_menu_item_text3[] = "Third Menu";
const char PROGMEM main_menu_item_text4[] = "Fourth Menu";
const MENU_ITEM_TYPE PROGMEM main_menu_items[] =
{
{main_menu_item_text1, &todo_menu, NULL},
{main_menu_item_text2, &todo2_menu, NULL},
{main_menu_item_text3, &todo3_menu, NULL},
{main_menu_item_text4, &todo4_menu, NULL}
};
const MENU_TYPE PROGMEM main_menu =
{
main_menu_text,
main_menu_items,
LENGTH(main_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO Menu"
const char PROGMEM todo_menu_text[] = "TODO Menu";
const char PROGMEM todo_menu_item_text1[] = "todo";
const MENU_ITEM_TYPE PROGMEM todo_menu_items[] =
{
{todo_menu_item_text1, &todo2_menu, NULL}
};
const MENU_TYPE PROGMEM todo_menu =
{
todo_menu_text,
todo_menu_items,
LENGTH(todo_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO2 Menu"
const char PROGMEM todo2_menu_text[] = "TODO2 Menu";
const char PROGMEM todo2_menu_item_text1[] = "todo2";
const MENU_ITEM_TYPE PROGMEM todo2_menu_items[] =
{
{todo2_menu_item_text1, &todo3_menu, NULL}
};
const MENU_TYPE PROGMEM todo2_menu =
{
todo2_menu_text,
todo2_menu_items,
LENGTH(todo2_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO3 Menu"
const char PROGMEM todo3_menu_text[] = "TODO3 Menu";
const char PROGMEM todo3_menu_item_text1[] = "todo3";
const MENU_ITEM_TYPE PROGMEM todo3_menu_items[] =
{
{todo3_menu_item_text1, &todo4_menu, NULL}
};
const MENU_TYPE PROGMEM todo3_menu =
{
todo3_menu_text,
todo3_menu_items,
LENGTH(todo3_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO4 Menu"
const char PROGMEM todo4_menu_text[] = "TODO4 Menu";
const MENU_TYPE PROGMEM todo4_menu =
{
todo4_menu_text,
NULL,
0,
MENU_INPUT_NUM
};

这段代码让我烦恼的是“extern”关键字。我需要它来编译,但由于结构是在同一个文件中定义的,所以感觉应该有更好的方法来编写代码。我绝对不想将所有声明都放在头文件中,因为使用终端的代码都不应该直接访问这些结构。通常我会克服被窃听的困扰并继续编码,但这个来源实际上会被客户看到。

有没有办法可以在不使用“extern”关键字的情况下编写此代码?

最佳答案

在标准 C 中,结构体并不等同于 typedef
此修改后的代码可在标准 C 编译器上编译:

typedef void (*MENU_FUNCTION_TYPE)();

typedef enum
{
MENU_INPUT_NAV, // Menu just navigates to other menus.
MENU_INPUT_ACTION, // Menu displays the results of an action.
MENU_INPUT_NUM, // Menu take numeric input and processes it.
NUM_MENU_INPUTS
} MENU_INPUT_TYPE;

// A menu is comprised of a list of menu items which you select with the number keys.
// This structure defines a menu and the menu items it contains.
typedef struct tag_MENU_ITEM_TYPE MENU_ITEM_TYPE;
typedef struct tag_MENU_TYPE
{
PGM_P text; // Pointer to text to display as menu name.
const MENU_ITEM_TYPE* menu_items; // Pointer to structures describing each menu item in this menu.
uint8_t num_items; // The number of menu item in this menu.
MENU_INPUT_TYPE type; // Specifies how this menu processes keyboard input.
} MENU_TYPE;

// This structure defines a menu item and what it does when you select it.
typedef struct tag_MENU_ITEM_TYPE
{
PGM_P text; // Pointer to text to display as menu item name.
const MENU_TYPE* link; // Pointer to which menu selecting this menu item takes you.
MENU_FUNCTION_TYPE action; // Pointer to function that is executed when this menu item is selected.
} MENU_ITEM_TYPE;

//These now acts as forward declarations
const MENU_TYPE PROGMEM main_menu;
const MENU_TYPE PROGMEM todo_menu;
const MENU_TYPE PROGMEM todo2_menu;
const MENU_TYPE PROGMEM todo3_menu;
const MENU_TYPE PROGMEM todo4_menu;

// **************************
// "Main Menu"
const char PROGMEM main_menu_text[] = "Main Menu";
const char PROGMEM main_menu_item_text1[] = "First Menu";
const char PROGMEM main_menu_item_text2[] = "Second Menu";
const char PROGMEM main_menu_item_text3[] = "Third Menu";
const char PROGMEM main_menu_item_text4[] = "Fourth Menu";
const MENU_ITEM_TYPE PROGMEM main_menu_items[] =
{
{main_menu_item_text1, &todo_menu, NULL},
{main_menu_item_text2, &todo2_menu, NULL},
{main_menu_item_text3, &todo3_menu, NULL},
{main_menu_item_text4, &todo4_menu, NULL}
};
const MENU_TYPE PROGMEM main_menu =
{
main_menu_text,
main_menu_items,
LENGTH(main_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO Menu"
const char PROGMEM todo_menu_text[] = "TODO Menu";
const char PROGMEM todo_menu_item_text1[] = "todo";
const MENU_ITEM_TYPE PROGMEM todo_menu_items[] =
{
{todo_menu_item_text1, &todo2_menu, NULL}
};
const MENU_TYPE PROGMEM todo_menu =
{
todo_menu_text,
todo_menu_items,
LENGTH(todo_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO2 Menu"
const char PROGMEM todo2_menu_text[] = "TODO2 Menu";
const char PROGMEM todo2_menu_item_text1[] = "todo2";
const MENU_ITEM_TYPE PROGMEM todo2_menu_items[] =
{
{todo2_menu_item_text1, &todo3_menu, NULL}
};
const MENU_TYPE PROGMEM todo2_menu =
{
todo2_menu_text,
todo2_menu_items,
LENGTH(todo2_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO3 Menu"
const char PROGMEM todo3_menu_text[] = "TODO3 Menu";
const char PROGMEM todo3_menu_item_text1[] = "todo3";
const MENU_ITEM_TYPE PROGMEM todo3_menu_items[] =
{
{todo3_menu_item_text1, &todo4_menu, NULL}
};
const MENU_TYPE PROGMEM todo3_menu =
{
todo3_menu_text,
todo3_menu_items,
LENGTH(todo3_menu_items, MENU_ITEM_TYPE PROGMEM),
MENU_INPUT_NAV
};

// **************************
// "TODO4 Menu"
const char PROGMEM todo4_menu_text[] = "TODO4 Menu";
const MENU_TYPE PROGMEM todo4_menu =
{
todo4_menu_text,
NULL,
0,
MENU_INPUT_NUM
};

关于c - 声明和定义相互引用的初始化常量结构的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com