Question

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"

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top