联合体可以在声明中初始化吗?
例如,假设我们有一个 union,
typedef union {
unsigned long U32;
float f;
}U_U32_F;
当声明这个 union 类型的变量时,有没有办法设置一个初始值?
U_U32_F u = 0xffffffff; // Does not work...is there a correct syntax for this?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用初始值设定项列表:
您可以通过以下方式设置第一个成员之外的其他成员
Use an initializer list:
You can set other members than the first one via
请注意,每个成员联合初始化在 C99 之前的编译器上不起作用,其中的数量令人沮丧。例如,当前的 Microsoft C 编译器不支持它。 (我隐约记得它甚至不支持第一个成员初始化,这可以追溯到 K&R II,但我可能是错的。)
Note that per-member union initialization doesn't work on pre-C99 compilers, of which there is a depressing number out there. The current Microsoft C compiler doesn't support it, for example. (I vaguely recall it doesn't even support first-member initialization, which goes back to K&R II, but I might be wrong about that.)
尝试
U_U32_F u = {0xffffffff};
Try
U_U32_F u = {0xffffffff};