对联合的不正确的访问Improper Access to a Union

ANSI 3.3.2.3 使用不同类型的成员访问联合对象的成员ANSI 3.3.2.3 A member of a union object is accessed using a member of a different type

如果声明两个类型的联合并存储一个值,但使用其它类型访问该联合,则结果是不可靠的。If a union of two types is declared and one value is stored, but the union is accessed with the other type, the results are unreliable.

例如,声明 float 和 int 的联合。For example, a union of float and int is declared. 存储一个 float 值,但程序稍后会将该值作为 int 进行访问。A float value is stored, but the program later accesses the value as an int. 在这种情况下,值取决于 float 值的内部存储。In such a situation, the value would depend on the internal storage of float values. 整数值是不可靠的。The integer value would not be reliable.

另请参阅See Also

结构、联合、枚举和位域Structures, Unions, Enumerations, and Bit Fields