我想执行以下操作
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/