[英]Const self-referential structures
I'm writing in C. 我在用C写作
I've defined a new type (noFunc_menuEntry) which is made of a self-referential structure. 我已经定义了一个由自引用结构组成的新类型(noFunc_menuEntry)。
struct noFunc_menuEntry_tag {
const char *text;
struct noFunc_menuEntry_tag *up_entry;
struct noFunc_menuEntry_tag *down_entry;
struct noFunc_menuEntry_tag *back_entry;
struct noFunc_menuEntry_tag *enter_entry;
};
typedef struct noFunc_menuEntry_tag noFunc_menuEntry;
I need to define a series of variable like this: 我需要定义一系列变量,如下所示:
menuEntry_1 = {title_1, &menuEntry_2, &menuEntry_4, &menuEntry_1, &menuEntry_5};
menuEntry_2 = {title_2, &menuEntry_3, &menuEntry_1, &menuEntry_1, &menuEntry_6};
and so on. 等等。
So i need to separate declaration and defition of the variable, because every variable depends on other variables. 所以我需要分离变量的声明和定义,因为每个变量都依赖于其他变量。 In an header file i've written the declaration
在头文件中,我写了声明
noFunc_menuEntry menuEntry_1, menuEntry_2, menuEntry_3, menuEntry_4, menuEntry_5, menuEntry_6;
etc..., and in a .c file in a function i've initialized the variables: etc ...,并在函数中的.c文件中初始化变量:
void menu_init(void)
{
menuEntry_1.text = title;
menuEntry_1.up_entry = &menuEntry_2
}
and so on for the other members and variables. 等等其他成员和变量。
However i want my variables to be const
: 但是我希望我的变量是
const
:
const noFunc_menuEntry menuEntry_1, menuEntry_2, menuEntry_3, menuEntry_4, menuEntry_5, menuEntry_6;
So my question is about separing declaration and definition of const
variables of the type i've defined. 所以我的问题是关于separing声明和我定义的类型的
const
变量的定义。 How can i do? 我能怎么做? Am i doing something wrong?
难道我做错了什么?
Naturally if i simply add const
in the declaration, the compiler report me an error when i initialize the variables (i'm trying to write read-only variables). 当然,如果我只是在声明中添加
const
,编译器会在初始化变量时报告错误(我正在尝试编写只读变量)。
If you want these variables to be const
, then you must do the initialization without the function. 如果您希望这些变量为
const
,则必须在没有该函数的情况下进行初始化。
But first, let's handle the const
in type definition: 但首先,让我们处理类型定义中的
const
:
typedef struct noFunc_menuEntry_tag noFunc_menuEntry;
struct noFunc_menuEntry_tag {
const char *text;
const noFunc_menuEntry *up_entry;
const noFunc_menuEntry *down_entry;
const noFunc_menuEntry *back_entry;
const noFunc_menuEntry *enter_entry;
};
Then the declarations for the header file: 然后是头文件的声明:
extern const noFunc_menuEntry menuEntry_1;
extern const noFunc_menuEntry menuEntry_2;
...
And finally the definition and initialization in source file: 最后是源文件中的定义和初始化:
const noFunc_menuEntry menuEntry_1 = {title_1, &menuEntry_2, &menuEntry_4, &menuEntry_1, &menuEntry_5};
const noFunc_menuEntry menuEntry_2 = {title_2, &menuEntry_3, &menuEntry_1, &menuEntry_1, &menuEntry_6};
...
You could use an array , since a bunch of item_i
implies you may need to. 你可以使用一个数组 ,因为你可能需要一堆
item_i
。
typedef struct noFunc_menuEntry_tag {
const char *text;
const struct noFunc_menuEntry_tag *up_entry;
const struct noFunc_menuEntry_tag *down_entry;
const struct noFunc_menuEntry_tag *back_entry;
const struct noFunc_menuEntry_tag *enter_entry;
} noFunc_menuEntry;
int main(void) {
const noFunc_menuEntry menuEntry[4] = {
{"", &menuEntry[0], &menuEntry[1], &menuEntry[2], &menuEntry[3]},
{"", &menuEntry[0], &menuEntry[1], &menuEntry[2], &menuEntry[3]},
{"", &menuEntry[0], &menuEntry[1], &menuEntry[2], &menuEntry[3]},
{"", &menuEntry[0], &menuEntry[1], &menuEntry[2], &menuEntry[3]},
};
return 0;
}
Use extern const
in the declaration in the header. 在标题的声明中使用
extern const
。 extern
will only declare the variable for use in the program, without allocating any memory. extern
只会声明变量在程序中使用,而不分配任何内存。 Then proceed with defining it in the main const noFunc_menuEntry menuEntry_1 = {title_1, &menuEntry_2, &menuEntry_4, &menuEntry_1, &menuEntry_5}; .. so on
然后继续在主
const noFunc_menuEntry menuEntry_1 = {title_1, &menuEntry_2, &menuEntry_4, &menuEntry_1, &menuEntry_5}; .. so on
const noFunc_menuEntry menuEntry_1 = {title_1, &menuEntry_2, &menuEntry_4, &menuEntry_1, &menuEntry_5}; .. so on
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.