Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!henry From: henry@utzoo.UUCP (Henry Spencer) Newsgroups: net.lang.c Subject: Re: Notes on the ANSI standard Message-ID: <4507@utzoo.UUCP> Date: Sat, 20-Oct-84 21:43:23 EDT Article-I.D.: utzoo.4507 Posted: Sat Oct 20 21:43:23 1984 Date-Received: Sat, 20-Oct-84 21:43:23 EDT References: <553@asgb.UUCP> Organization: U of Toronto Zoology Lines: 23 > Section 4.1.3 states: > > "With one exception, a member of a union object may not be > inspected unless the value of the object has been assigned > using that same member." > > HOLY!! Whatever happened to overlaying values of differing > types? One of the more handy uses of unions is overlaying bitfields > with an integer so that the bits can be twiddled individually, but the > integer can be used for block operations on all the bits. I've also > used similar techniques for decoding bit fields of devices like a > Summagraphic Bitpad, or several mouses. Unions were intended as a storage-allocation-control mechanism, not as a way to do type cheating. The orthodox way to do the latter is casts. Incidentally, using integers and bit operations (yes, I know it's ugly) is probably more portable than bitfields. Believe it or not. Partly because a good many compilers botch bitfields, partly because things like the bit allocation of bitfields are very compiler-dependent. -- Henry Spencer @ U of Toronto Zoology {allegra,ihnp4,linus,decvax}!utzoo!henry