可以在声明中初始化联合吗? [英] Can a union be initialized in the declaration?
本文介绍了可以在声明中初始化联合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,假设我们有一个联合
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 there a way to set an initial value?
U_U32_F u = 0xffffffff; // Does not work...is there a correct syntax for this?
推荐答案
使用初始化列表:
U_U32_F u = { 0xffffffff };
你可以设置除第一个成员之外的其他成员
You can set other members than the first one via
U_U32_F u = { .f = 42.0 };
这篇关于可以在声明中初始化联合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文