【问题标题】:Compiler Error when using a struct使用结构时的编译器错误
【发布时间】:2025-12-21 03:30:15
【问题描述】:

我在初始化一个结构时遇到了一个奇怪的编译器错误。

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

根据几个不同的教程,这对我来说看起来不错,但是在两台不同的机器上,编译时出现以下错误:

testserver.c:15:9: 错误:在“.”令牌之前需要“=”、“,”、“;”、“asm”或“attribute
testserver.c:16:9:错误:在“.”标记之前需要“=”、“,”、“;”、“asm”或“attribute
testserver.c:17:9: 错误:在“.”标记之前需要“=”、“,”、“;”、“asm”或“attribute
testserver.c:18:9:错误:在“.”标记之前应为“=”、“,”、“;”、“asm”或“attribute

为什么会出现这个错误?

【问题讨论】:

  • 你能粘贴整个代码sn-p吗?包括这出现在哪个函数中..
  • 看看行号——我认为这他真正的代码(我想他可能也暂时忘记了这个功能,但是,也许不是...... )

标签: c compiler-construction compiler-errors


【解决方案1】:
sendData.az = 25;

这样的语句必须在函数内部。如果你想初始化结构,有不同的语法:

struct RadarData sendData = { 25, 10, 1, 100 };

【讨论】:

    【解决方案2】:

    如果我正确地查看了您的代码(这是完整的相关代码),那么您就是在函数之外放置语句。这是不对的。

    【讨论】: