在C ++和C初始化联盟 [英] Union initialization in C++ and C

查看:120
本文介绍了在C ++和C初始化联盟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个工作的C库,使用常量,在定义为

I have built a working C library, that uses constants, in header files defined as

typedef struct Y {
  union {
    struct bit_field bits;
    uint8_t raw[4];
  } X;
} CardInfo;

static const CardInfo Y_CONSTANT = { .raw = {0, 0, 0, 0 } };

我知道 .RAW 初始化是C仅语法。

我如何的方式,这样我可以在C和C使用它们++定义在他们工会的常量。

How do I define constants with unions in them in a way such that I can use them in C and C++.

推荐答案

我有同样的问题。对于C89以下是正确的:

I had the same problem. For C89 the following is true:

使用C89风格的初始化,结构成员必须在初始化
  命令宣布,只有联合的第一个成员可以是
  初始化

With C89-style initializers, structure members must be initialized in the order declared, and only the first member of a union can be initialized

我发现这个解释的:
<一href=\"https://www-01.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.cbclx01/strin.htm\"相对=nofollow>结构和联合的初始化

这篇关于在C ++和C初始化联盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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