Common initial sequence in structures nested within union - definition in C standard

后端 未结 1 830
自闭症患者
自闭症患者 2020-12-21 11:09

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

6.5.2.3/6

相关标签:
1条回答
  • 2020-12-21 11:44

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

    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.

    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.

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

    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.

    0 讨论(0)
提交回复
热议问题