I’m writing a small language in C, and it requires variable setting. I have a variable table set up, but I’m getting a strange error.
#define VAR_SIZE 100
typedef struct {
const char *key;
int value;
} variable;
variable Table[VAR_SIZE];
Table[0].key = NULL;
Table[0].value = 0;
When I run that, I get the following error:
stack.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
stack.c:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
What’s going on?
You’re trying to write code at global scope. You can’t do that. You either need to give your global variable a static initializer, or initialize it at runtime inside of a function call.
An example of a static initializer:
Initializing at runtime:
If you’re compiling in C99 mode, you can also use a designated initializer: