union containing only one struct

There is no benefit in standard portable C.

But code like this is used to circumvent (in a non-portable way) all the type checking that your C compiler will make.

You are then empowered to set all the members of the underlying struct in one go, which is useful in this case as it contains a lot of bit fields.


It does not make any difference if you wrap and I suppose that someone has forgoten to add another member (or did not copy-paste everything) as in the declaration below. No warnings will be suppressed.

typedef union {
    struct {
        unsigned ANS0       :1;
        unsigned ANS1       :1;
        unsigned ANS2       :1;
        unsigned ANS3       :1;
        unsigned ANS4       :1;
        unsigned ANS5       :1;
        unsigned ANS6       :1;
    };
    uint8_t d8;
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);

BTW if the struct has to fit in 1 byte (8 bits) this declaration is wrong and uint_t type should be used instead.

Tags:

C

Struct

Unions