Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const self-referential structures

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).

like image 482
BzFr Avatar asked Aug 17 '16 06:08

BzFr


People also ask

What is self-referential structure in C language?

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++.

Which one is a self-referential data type?

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.

Which is self-referential data structure Mcq?

1 Answer. A structure contains an element that is a pointer to the same structure is called self referential structure.

Can a structure can point to itself?

Explanation: A structure pointing to itself is called self-referential structures.


2 Answers

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};
 ...
like image 97
user694733 Avatar answered Oct 11 '22 14:10

user694733


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;
}
like image 26
StoryTeller - Unslander Monica Avatar answered Oct 11 '22 13:10

StoryTeller - Unslander Monica