Is type-punning through a union unspecified in C99, and has it become specified in C11?

后端 未结 4 1253
野的像风
野的像风 2020-11-22 04:17

A number of answers for the Stack Overflow question Getting the IEEE Single-precision bits for a float suggest using a union structure for type punning (e.g.: t

4条回答
  •  日久生厌
    2020-11-22 05:00

    The original C99 specification left this unspecified.

    One of the technical corrigenda to C99 (TR2, I think) added footnote 82 to correct this oversight:

    If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.

    That footnote is retained in the C11 standard (it's footnote 95 in C11).

提交回复
热议问题