简体   繁体   中英

Compiler Error when using a struct

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?

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 };

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM