0

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?

2
  • can you paste the entire code snippet? Including which function this appears in.. Commented Jul 17, 2011 at 21:39
  • look at the line numbers - I think this is his real code (I thought he might be forgetting the function for a moment as well, but, perhaps not...) Commented Jul 17, 2011 at 21:40

2 Answers 2

9
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 };
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.