Jetmax
Jetmax

Reputation: 53

Global struct in c (initializer element is not a compile-time constant)

I am pretty new to C and I have some problems.

I have the following struct definition:

struct env {
  struct env *next;
  char varName[8];
  int n;
};

I want to create multiple of those structs eventually in some of my functions so I created a function to do that :

struct env *mkEnv(char c[8] , int value , struct env *toadd){
    struct env *enviroment = malloc(sizeof(struct env));
    enviroment->n = value;
    enviroment->next = toadd;
    strcpy(enviroment->varName , c);
    return enviroment;
}

I want to create a struct like this globally that is constant with some constant values and initially have the pointer to the next structure NULL.

So I did something like this :

    //not in a function
    struct env *list = mkEnv("pot" , 0 , NULL);

    //Beginning of a function
    int eval(struct expression *exp){
        ... // code here that might add a new structure to the pointer of list
    }

I get the following error however :

evalexp.c:116:20: error: initializer element is not a compile-time constant struct env *list = mkEnv("p" , 0 , NULL);

I understand what this error means after googling about the error message but is there a way to create a structure somewhere outside of a function without getting this compilation error?

To make it more clear : I want to create a structure as defined above (as if it was the head of a list). so that all my functions can access and add stuff to it. That is parsing it as a list and/or adding new elements in that list.

Thanks in advance!

Upvotes: 3

Views: 2730

Answers (3)

Frankie_C
Frankie_C

Reputation: 4877

You can use the following syntax:

struct env
{
  struct env *next;
  char varName[8];
  int n;
};
struct env *list = &((struct env){ NULL, "pot" , 0});

Note the cast to structure applied to the initializer and addess of operator & applied to it ;-)
In detail the part: (struct env){ NULL, "pot" , 0} tells the compiler that the initializer { NULL, "pot" , 0} is referred to a struct struct env, this instruct compiler to create an initialized instance of a struct env. Then we get the address of this structure, using the & operator, and assign it to the pointer list.
Works with C99 and C11 compilers.

Upvotes: 1

2501
2501

Reputation: 25753

A function cannot be called in file scope.

You must use constant values and define an actual struct variable:

struct env lists = { NULL , "pot" , 0 };
struct env *list = &lists;

Now the pointer list is initialized and can be used, but beware that it wasn't created with malloc, so it cannot be freed or reallocated.

Upvotes: 5

Joni
Joni

Reputation: 111389

Define the variable, and initialize it in main.

struct env *list;

int main() {
    list = mkEnv("pot" , 0 , NULL);
    ....
}

Sometimes libraries make you call a function to initialize the library before any of its functions should be used. Giving values to global variables that they use is one of the reasons why they do that.

Upvotes: 4

Related Questions