Can a union be initialized in the declaration?

后端 未结 3 1778
庸人自扰
庸人自扰 2020-11-29 08:12

For example, say we have a union

typedef union {
unsigned long U32;
float f;
}U_U32_F;

When a variable of this union type is declared, is t

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 08:49

    Use an initializer list:

    U_U32_F u = { 0xffffffff };
    

    You can set other members than the first one via

    U_U32_F u = { .f = 42.0 };
    

提交回复
热议问题