Unions and type-punning

后端 未结 5 2114
广开言路
广开言路 2020-11-22 04:46

I\'ve been searching for a while, but can\'t find a clear answer.

Lots of people say that using unions to type-pun is undefined and bad practice. Why is this? I can\

5条回答
  •  庸人自扰
    2020-11-22 05:20

    It's legal in C99:

    From the standard: 6.5.2.3 Structure and union members

    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.

提交回复
热议问题