8

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

user694733
  • 15,208
  • 2
  • 42
  • 68
BzFr
  • 83
  • 1
  • 6
  • If you already init the variables with `menuEntry_1 = {title_1, &menuEntry_2, &menuEntry_4, &menuEntry_1, &menuEntry_5};`, why would you initialize them again in `menu_init`? – user694733 Aug 17 '16 at 07:05
  • Since your struct contains *pointers* to these (global) variables, I believe you should be able to simply define them and initialize in a single line, i.e. there is no need to have functions for initialization. It's true that *"every variable depends on other variables"*, but if you are changing `menuEntry_1` contents, it doesn't mean `&menuEntry_1` will change. – vgru Aug 17 '16 at 07:15
  • @user694733 : I initialize the variables in `menu_init` if i don't use them as a `const`, but if they have to be a `const` naturally i don't use `menu_init`. I was looking for the right way to do it! – BzFr Aug 17 '16 at 14:35

3 Answers3

6

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};
 ...
user694733
  • 15,208
  • 2
  • 42
  • 68
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;
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
1

Use extern const in the declaration in the header. extern will only declare the variable for use in the program, without allocating any memory. 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

Sashank
  • 107
  • 2
  • 11