Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!lll-tis!helios.ee.lbl.gov!nosc!ucsd!ucsdhub!hp-sdd!hplabs!hpda!hpcuhb!hpcllla!hpcllca!walter From: walter@hpcllca.HP.COM (Walter Murray) Newsgroups: comp.lang.c Subject: Re: Union type conversions Message-ID: <7330008@hpcllca.HP.COM> Date: 22 Jul 88 23:50:17 GMT References: <494@aiva.ed.ac.uk> Organization: HP NSG/ISD Computer Language Lab Lines: 30 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 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.