我在其中使用了一个union和一个匿名结构。我想知道如何访问其中的结构成员。就像我想使用union变量访问结构的G变量那样我该怎么做呢。请帮助:)
typedef union
{
uint8_t All;
struct
{
uint8_t G:1;
uint8_t O_B:1;
uint8_t Unused:1;
uint8_t W1:1;
uint8_t W2:1;
uint8_t Y1:1;
uint8_t Y2:1;
uint8_t IAQ:1;
};
} _UNION_VARIABLE;
我遇到了访问结构成员的编译错误,如下所示:
_UNION_VARIABLE sg_RefreshVar;
sg_RefreshVar.G = false;
错误:
#137 union "<unnamed>" has no field "G"
答案 0 :(得分:2)
你需要给它一个名字
typedef union
{
uint8_t All;
struct
{
uint8_t G:1;
uint8_t O_B:1;
uint8_t Unused:1;
uint8_t W1:1;
uint8_t W2:1;
uint8_t Y1:1;
uint8_t Y2:1;
uint8_t IAQ:1;
} bit_field;
} _UNION_VARIABLE;
然后你可以去
_UNION_VARIABLE.bit_field.G;