Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bbn!rmasters From: rmasters@bbn.COM (Bob Masters) Newsgroups: comp.lang.c Subject: Re: initialization of unions Message-ID: <4237@ccv.bbn.COM> Date: Tue, 27-Oct-87 10:16:36 EST Article-I.D.: ccv.4237 Posted: Tue Oct 27 10:16:36 1987 Date-Received: Thu, 29-Oct-87 22:15:42 EST References: <165600017@uiucdcsb> Reply-To: kgregory@ccy.bbn.com (Keith D Gregory) Organization: Bolt Beranek and Newman Inc., Cambridge MA Lines: 41 Summary: They may be initialized In article <165600017@uiucdcsb> wsmith@uiucdcsb.cs.uiuc.edu writes: > >I have a question about initialization of variables in C. >Kernighan & Ritchie do not speak of what happens when a union is >attempted to be initialized. The 4 compilers available to me disagree on >what is legal. ANSI C has a specific way of initializing unions, which, as I recall is so ambiguously worded as to be almost useless (allow 20 minutes to decipher :-) As I recall (this was from several months ago, when I was writing a book on Turbo-C . . . they didn't follow the standard . . .), unions are initialized as if you were initializing their first element. So . . . union u1 { long along; int anint[2]; } myunion = { 0x012345678L }; union u2 { int anint[2]; long along; } yourunion = { 0x5678, 0x1234 }; . . . are both valid declarations, and (on a small-endian machine, such as an '86) are pretty much equivalent. > >struct foo { > union { > char * pch; > int a; > } j; > int l; > } unionvar = { "hello\n", 37 }; > I believe that this should work on an ANSI-compatible compiler -kdg