I don’t understand why my C program does not compile.
The error message is:
$ gcc token_buffer.c -o token_buffer
token_buffer.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
The first structure – token is intended to be used in many places, so I use an optional structure tag. The second structure declaration I am not reusing anywhere else so I am not using a structure tag but instead I define a variable named buffer.
And then compilation fails when I try to assign a value to one of the members of this structure.
Help?
/*
* token_buffer.c
*/
#include <stdio.h>
#include <stdbool.h>
/* A token is a kind-value pair */
struct token {
char *kind;
double value;
};
/* A buffer for a token stream */
struct {
bool full;
struct token t;
} buffer;
buffer.full = false;
main()
{
struct token t;
t.kind = "PLUS";
t.value = 0;
printf("t.kind = %s, t.value = %.2f\n", t.kind, t.value);
}
You cannot have free-standing operations in C: you need to put initialization into your
main.