嵌套在联合中的结构中的公共初始序列 - C 标准中的定义 [英] Common initial sequence in structures nested within union - definition in C standard

查看:30
本文介绍了嵌套在联合中的结构中的公共初始序列 - C 标准中的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C11 标准中,嵌套在单个联合中的结构共享的公共初始序列的定义如下:

In C11 standard there is following definition of common initial sequence shared by structures nested within a single union:

6.5.2.3/6

为了简化联合的使用,做了一个特殊的保证:如果联合包含多个共享公共首字母的结构序列(见下文),如果联合对象当前包含一个在这些结构中,允许检查共同的初始它们中的任何一个的一部分,即已完成类型的声明联盟是可见的.两个结构共享一个公共首字母序列 如果相应的成员具有兼容的类型(并且,对于位域,相同的宽度)用于一个或多个初始会员.

One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.

示例 3 以下是有效片段:

EXAMPLE 3 The following is a valid fragment:

union {
    struct {
        int alltypes;
    } n;

    struct {
        int type;
        int intnode;
    } ni;

    struct {
        int type;
        double doublenode;
    } nf;
} u;

u.nf.type = 1;
u.nf.doublenode = 3.14;
/* ... */
if (u.n.alltypes == 1)
        if (sin(u.nf.doublenode) == 0.0)
            /* ... */

根据我对本文的理解,上面的代码是无效的.

According to my understanding of this article, the above code, however, is invalid.

在外部 if 语句中,我们表明 n::alltypes 数据成员是活动的(同时与 ni::typenf::type 作为标准状态)但在内部 if 中我们使用 nf::doublenode 它不是公共初始序列的一部分.

In the outer if statement we indicate that n::alltypes data member is active (simultaneously with ni::type and nf::type as the standard states) yet in the inner if we use nf::doublenode which is not a part of the common initial sequence.

有人可以澄清这个问题吗?

Can somebody clarify this issue?

推荐答案

允许检查[共享一个公共初始序列的几个结构]的公共初始部分

it is permitted to inspect the common initial part of [several structures that share a common initial sequence]

使用提供的示例,规范的这一部分是说由于 union 的每个可能的成员类型都有一个 int 作为初始字段,您可以访问它使用任何成员类型的公共初始字段,即使在变量已被初始化/用作特定成员类型之一之后.

Using the provided example, this part of the specification is saying that since each possible member type of the union has an int as the initial field, you can access that common initial field using any of the member types, even after the variable has been initialized/used as one of the specific member types.

这正是示例所做的:它访问初始 int 作为 nalltypes 成员,after 已将以下字段初始化为 nf,然后继续访问 nfdoublenode 字段,所有这些都使用相同的变量.

This is just what the example does: it accesses the initial int as the alltypes member of an n, after having initialized following fields as an nf, and then goes on to access the doublenode field of an nf, all using the same variable.

使用 union 作为一种可能的类型并不会强迫它进入某种结构:这就是联合的​​工作方式.

Using the union as one of the possible types doesn't force it into some sort of structure: this is how unions work.

请注意,此保证已存在一段时间:在 ANSI 规范,部分:3.3.2.3 结构和联合成员.

Note that this guarantee has been around for some time: essentially the same text is found in the ANSI specification, section: 3.3.2.3 Structure and union members.

这篇关于嵌套在联合中的结构中的公共初始序列 - C 标准中的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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