Union type conversions

Walter Murray walter at hpcllca.HP.COM
Sat Jul 23 09:50:17 AEST 1988


Chris Torek writes:

>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.  

O.K., how about:

#include <assert.h>
main ()
{
   struct {int a; int b;} s;
   union  {int c; int d;} u;
   assert (&s.a <  &s.b);
   assert (&u.c == &u.d);
}

I believe the draft proposed ANSI standard guarantees this to work
(May, 1988, section 3.3.8).  "Pointers to structure members declared
later compare higher than pointers to members declared earlier in
the structure."   "All pointers to members of the same union object
compare equal."

Walter Murray
All opinions expressed are my own.



More information about the Comp.lang.c mailing list