Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!apple!rutgers!cmcl2!yale!wald-david From: wald-david@CS.YALE.EDU (david wald) Newsgroups: comp.lang.c Subject: Re: Union initialization Message-ID: <51116@yale-celray.yale.UUCP> Date: 19 Feb 89 20:55:44 GMT References: <816@atanasoff.cs.iastate.edu> <16019@mimsy.UUCP> Sender: root@yale.UUCP Reply-To: wald-david@CS.YALE.EDU (david wald) Organization: Yale University Computer Science Dept, New Haven CT 06520-2158 Lines: 41 In article <16019@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >In article <816@atanasoff.cs.iastate.edu> hascall@atanasoff.cs.iastate.edu >(John Hascall) writes: >>Does 'ANSI' C allow for union initialization? > >The rule is (perhaps overly) simple: the first member of the union >is initialised. Given > > union { float f; int i; } u; > >u.f is 0.0, and u.i is indeterminte. You may write > > union { float f; int i; } u = { 1.0 }; > >to set u.f, but you cannot initialise u.i since it is not the first >member. I wonder... Yes, this question deals with some hypothetical C' or C+=2 (not quite D, since it's a language extension rather than a revision), but... Would it make the syntax more ambiguous to have allowed union { float f; int i; } u.i = {1}; ? The only difficulty comes in extending this to structures containing unions: struct { char *cp; union { float f; int i; } u; } s = { NULL, ????}; Any suggestions? ============================================================================ David Wald wald-david@yale.UUCP waldave@yalevm.bitnet wald-david@cs.yale.edu "A monk, a clone and a ferengi decide to go bowling together..." ============================================================================