Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ico!ism780c!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Type punning in C Message-ID: <14939@haddock.ima.isc.com> Date: 20 Oct 89 19:03:32 GMT References: <475@idacrd.UUCP> <11242@smoke.BRL.MIL> <605@maytag.waterloo.edu> <6310@arcturus> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 18 In article <6310@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes: >I thought that the standard said (as opposed to what I remember reading >in K&R I) that members of unions do NOT have to have zero offset? The pANS guarantees that a pointer to a union, suitably cast, points to each of its members, and vice versa. It also says that the behavior is undefined if you use a union for punning. (What else could it say? You can't close your eyes and pretend that the behavior is predictable!) Since you already know your program isn't portable, you needn't worry about this. It may be that the undefined-behavior clause would permit an implementation where punning doesn't work and differently-typed union members don't overlap, provided that appropriate code is generated when casting to/from a union type. This may be the source of the rumor you heard. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint