我想执行以下操作
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;
答案 0 :(得分:1)
与this question类似,请尝试Type2 t = { .Base.Data = 0x18 };
如果您的目标是在Type1
中设置位域,则应该直接将它们调出来;见here。