我的嵌入式 C99 兼容程序中有多个结构(模仿微芯片编译器使用的定义类型,尽管这不是微芯片应用程序)。这是一个典型的例子:
typedef struct
{
union
{
struct
{
CommandDirection_t ReadWrite : 1;
RegisterAddress_t Register : 7;
};
uint8_t Byte;
};
} MemsAccelCommand_t;
CommandDirection_t 和 RigisterAddress_t 是枚举。稍后在我的代码中声明并初始化我的结构:
MemsAccelCommand_t command = { .ReadWrite = CMD_Read };
此编译没有警告或错误,但是当我检查文件时,我收到错误:“错误 65:需要一个成员名称”。
我如何调整我的代码以便不再出现 lint 错误,或者我该怎么做才能为此禁用 lint 警告(除了禁用错误 65 之外)?
最佳答案
为 union 体和最内层结构命名,如下所示:
typedef struct
{
union
{
struct
{
CommandDirection_t ReadWrite : 1;
RegisterAddress_t Register : 7;
} Byte_struct;
uint8_t Byte;
} Byte_union;
} MemsAccelCommand_t;
否则,您打算如何访问 union 字段? union 必须有一个名字。
关于c - 匿名结构 + 复合文字导致 lint 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20889736/