如何初始化一个int埋在typdef'd联盟内部2层深处

时间:2012-03-05 18:48:08

标签: c struct typedef bit-fields unions

我想执行以下操作

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

作为静态初始化。例如

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

我会用什么来对付它? 以下是Type1和Type2的typedef

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;

1 个答案:

答案 0 :(得分:1)

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