Union type conversions

will summers will.summers at p6.f18.n114.z1.fidonet.org
Sat Jul 30 08:21:27 AEST 1988


 > I am not sure.  In particular, if there is no testable assertion that
 > makes a union different from a structure, then a compiler that implements
 > a union as a structure will not break any (testable) rules and will
 > thus be correct.
 >
 > Write some correct code that produces a wrong answer if a union of a
 > set of elements were implemented as a structure containing all the
 > elements, and you will have a proof.
 
How about:
  union { int a; int b; } x;
 
  int offset_of_b = (int) (&x.b - &x);
    ...
 
    if  (offset_of_b != 0)
        printf("compiler is broke\n");
 
"A union may be thought of as a structure all of whose members begin at offset 
0" (K&R A.8.5, pg 197).
 
If I may "think of" a union as an "all 0 offset structure", then I may write 
code that fails when any aspect of that metaphor is violated.
 
So what's wrong with the above code?
 
   \/\/ill
 


--  
St. Joseph's Hospital/Medical Center - Usenet <=> FidoNet Gateway
Uucp: ...ncar!noao!asuvax!stjhmc!18.6!will.summers
Internet: will.summers at p6.f18.n114.z1.fidonet.org



More information about the Comp.lang.c mailing list