c - 如何初始化在 typdef union 中深埋 2 层的 int

标签 c struct typedef bit-fields unions

我想执行以下操作

static Type2 MyData;
MyData.Base.Data = (0b0000000000010100);

作为静态初始化。例如

static Type2 MyData = (typecast)(0b0000000000010100);

我会用什么来打字? 这是 Type1 和 Type2 的类型定义

typedef union
{
    UINT16 Data;
    struct
    {
        unsigned      :10;  
        unsigned var1 :3;   
        unsigned var2 :2;   
        unsigned var3 :1;   
    };
} Type1;    

typedef union
{
    Type1 Base;     
    struct
    {
        unsigned var4 :3;
        unsigned var5 :2;   
        unsigned      :11;
    } Data;
} Type2;

最佳答案

类似于this question ,尝试 Type2 t = { .Base.Data = 0x18 }; 如果您的目标是在 Type1 中设置位域,您应该直接调用它们;见here .

关于c - 如何初始化在 typdef union 中深埋 2 层的 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9572144/

相关文章:

c - fscanf 不从文件中读取字符

c++ - 如何在不破坏代码的情况下正确修复 "zero-sized array in struct/union"警告(C4200)?

C程序。 : Structure as argument of function?

c - Structs 中的 Structs 按顺序排列

c++ - 整数数组的位封装

c++ - 我将如何使用 getopt 呢?

c - 这个 C 代码会泄漏内存吗?

malloc 中的 C 结构数组

c++ - 如何实例化 const typedef 指针?

c++ - 访问字节数组的 typedef 指针