문제

I am using a union and an anonymous structure inside it. I want to know how I can access the structure member inside it. Like if i want to access G variable of structure using union variable then how can i do that. Please help :)

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;

I am getting compilation error for accessing structure member like below :

_UNION_VARIABLE sg_RefreshVar; 

sg_RefreshVar.G = false;

Error : #137 union "<unnamed>" has no field "G"

도움이 되었습니까?

해결책

You need to give it a name

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;

and then you can go

_UNION_VARIABLE.bit_field.G;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top