Reputation: 117
I'm getting a strange compiler error initializing a struct.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct RadarData
{
unsigned int messageID : 32;
unsigned int time : 32;
float az;
float el;
};
struct RadarData sendData;
sendData.az = 25;
sendData.el = 10;
sendData.messageID = 1;
sendData.time = 100;
This looks fine to me according to a few different tutorials, but on two different machines, I'm getting the following error when compiling:
testserver.c:15:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:16:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:17:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:18:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
Why am I getting this error?
Upvotes: 0
Views: 151
Reputation: 3017
If I'm looking at your code right (and that's the complete relevant code), then you're placing statements outside of a function. That's not right.
Upvotes: 3
Reputation: 231063
sendData.az = 25;
Statements like this must be inside a function. If you want to initialize the struct, there's a different syntax for that:
struct RadarData sendData = { 25, 10, 1, 100 };
Upvotes: 9