Reputation: 355
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);
}
Upvotes: 1
Views: 273
Reputation: 78923
Assignement and initialization are two different things in C. Just do
struct {
bool full;
struct token t;
} buffer = { .full = false };
Upvotes: 0
Reputation: 121397
The offending part is: buffer.full = false;
as you set the value outside.
Put this statement inside main()
.
Upvotes: 1
Reputation: 726589
You cannot have free-standing operations in C: you need to put initialization into your main
.
int main() { // Don't forget to make your main return int explicitly
struct token t;
buffer.full = false; // <---- Here it is legal
t.kind = "PLUS";
t.value = 0;
printf("t.kind = %s, t.value = %.2f\n", t.kind, t.value);
return 0; // main should return status to the operating system
}
Upvotes: 4