c - 匿名结构 + 复合文字导致 lint 错误

标签 c struct anonymous-types lint compound-literals

我的嵌入式 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/

相关文章:

c - 为什么传递给库的函数指针(作为回调)在执行时不会导致段错误?

c - 在 C 中进行通用/类 OO 编程,同时避免符号冲突

c - 在其他文件中的结构/数组上使用 malloc

c# - 如何将 List<Anonymous Type> 转换为 List<string>

asp.net - 如何从嵌套转发器控件访问 linq group-by 查询的组?

c - 如何将 main 函数与头文件链接起来并创建它的 dll 文件?

java - 计算给定 n 的每行和每列中正好有 n/2 个零和 n/2 个的矩阵数

c - 是否可以创建嵌套结构数组?如何?

c - C99 语言中具有未命名成员的结构的正确行为是什么?

java - 是什么导致 SAXException2 : Instance of “com.foo.Bar” is substituting “java.lang.Object” , 但 “com.foo.Bar” 绑定(bind)到匿名类型