I'm writing in C.
I've defined a new type (noFunc_menuEntry) which is made of a self-referential structure.
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:
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 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. 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).
The self-referential structure is a structure that points to the same type of structure. It contains one or more pointers that ultimately point to the same structure. Structures are a user-defined data structure type in C and C++.
A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a self-referential data structure. The next node of a node is being pointed, which is of the same struct type.
1 Answer. A structure contains an element that is a pointer to the same structure is called self referential structure.
Explanation: A structure pointing to itself is called self-referential structures.
If you want these variables to be const
, then you must do the initialization without the function.
But first, let's handle the const
in type definition:
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With