Search code examples
cstructheader-filescc

Can't assign struct variable in header file


I have a header file including a structure like this:

typedef struct
{
    int index = -1;
    stack_node *head;
} stack;

But when compiling with cc it shows error at the assignment line (int index = -1):

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token

should I add an initialization function to initialize variables?


Solution

  • In C, you can't assign variables inside the struct.

    You should initialise them in another function when each instance is created, however.