In C90 6.3.2.3p5 (with my emphasis)
With one exception, if a member of a union object is accessed after a value has been stored in a different member of the object, the behavior is implementation-defined.” 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. 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.
The bold part isn't seen after C90. And I think the empty space is substituted for detailed aliasing rule and effective type. But I don't know whether the bold part is now well defined.
float test1( float *fp, unsigned *uip)
{
*fp = 1.0f;
*uip += 1;
return *fp;
}
union { float f; unsigned ui} u;
float test2(void)
{
return test1(&u.f, &u.ui);
}
In the function test1
, using the pointer fp
to access the member of the union is implementation-defined according to C90.
But is this now well defined regardless of implementation?
Can I know which part is related to understand this situation?