Search code examples
ccompiler-errors

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?


Solution

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