Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!ncar!noao!asuvax!stjhmc!p6.f18.n114.z1.fidonet.org!will.summers From: will.summers@p6.f18.n114.z1.fidonet.org (will summers) Newsgroups: comp.lang.c Subject: Re: Union type conversions Message-ID: <224.22F29448@stjhmc.fidonet.org> Date: 29 Jul 88 22:21:27 GMT Sender: ufgate@stjhmc.fidonet.org (newsout1.17) Organization: FidoNet node 1:114/18.6 - Iasd Eng Bbs, Phoenix Az Lines: 35 > 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@p6.f18.n114.z1.fidonet.org