Union of structs with common first member

后端 未结 3 951
醉话见心
醉话见心 2020-12-18 07:59

I am unsure of whether or not the code has pointer aliasing (or other standard conformance issues) in the asserts cast. It seems that a pointer to the union type should be a

3条回答
  •  爱一瞬间的悲伤
    2020-12-18 08:42

    That should be fine. C11, 6.5.2.3/6 ("Structure and union members") says:

    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.

    (C++ makes the same guarantee (C++11, 9.2/18) for standard-layout unions.)

提交回复
热议问题