Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site watmath.UUCP Path: utzoo!watmath!kpmartin From: kpmartin@watmath.UUCP (Kevin Martin) Newsgroups: net.lang.c Subject: Re: Standard for union initialization? Message-ID: <10974@watmath.UUCP> Date: Thu, 17-Jan-85 20:32:36 EST Article-I.D.: watmath.10974 Posted: Thu Jan 17 20:32:36 1985 Date-Received: Fri, 18-Jan-85 01:15:24 EST References: <6995@brl-tgr.ARPA> <7004@brl-tgr.ARPA> <6847@watdaisy.UUCP> <10884@watmath.UUCP> <4930@utzoo.UUCP> Reply-To: kpmartin@watmath.UUCP (Kevin Martin) Organization: U of Waterloo, Ontario Lines: 73 Summary: In article <4930@utzoo.UUCP> henry@utzoo.UUCP (Henry Spencer) asks: >> Either way, this is less clear than an initializer of the form >> element = value >> e.g. >> union { >>foo; >> bar; >> mumble; >> }baz = mumble = ; > >How do you use this to initialize a union inside a structure? struct foo { int bar; char *blech; union { float float_val; char char_val; long long_val; } onion; }; struct foo x = { 42, /* value for x.bar */ "value for x.blech", char_val = 'x' /* value for x.onion.char_val */ }; /* value for x */ -or- (adding full {}'s) struct foo x = { 42, /* value for x.bar */ "value for x.blech", { char_val = 'x' /* value for x.onion.char_val */ } /* value for x.onion */ }; /* value for x */ And, seeing as I see the next question coming... >How do yo initialize a structure inside a union? Well, this is where your YACC grammer may actually need changes (but this applies to the name-the-type suggestion too...) union foo { int bar; char *blech; struct { float fred; char herbie; long harry; } stroct; }; union foo x = { stroct = { 4.2, /* value for x.stroct.fred */ 'x', /* value for x.stroct.herbie */ 0x12345678 /* value for x.stroct.harry */ } /* value for x.stroct */ }; /* value for x */ -or- (removing extra {} ) union foo x = stroct = 4.2, /* value for x.stroct.fred */ 'x', /* value for x.stroct.herbie */ 0x12345678 /* value for x.stroct.harry */ ; /* value for x */ (You would need the inner {} in this example, say, if you wanted x.stroct.harry to be *implicitly* initialized to zero) Kevin Martin, UofW Software Development Group