能否工会在声明中初始化? [英] Can a union be initialized in the declaration?

查看:91
本文介绍了能否工会在声明中初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举例来说,假设我们有一个联盟

 的typedef工会{
无符号长U32;
浮F;
} U_U32_F;

在这个联盟类型的变量声明,有没有办法设置一个初始值?

  U_U32_F U = 0xffffffff的; //没有工作...有一个正确的语法呢?


解决方案

使用的初始化列表:

  U_U32_F U = {}为0xffffffff;

您可以通过

设置其他成员比第一次

  U_U32_F U = {.F = 42.0};

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?

解决方案

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 };

这篇关于能否工会在声明中初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆