Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utcsri!greg From: greg@utcsri.UUCP Newsgroups: comp.lang.c Subject: Re: union initialization Message-ID: <4665@utcsri.UUCP> Date: Sat, 25-Apr-87 17:26:12 EST Article-I.D.: utcsri.4665 Posted: Sat Apr 25 17:26:12 1987 Date-Received: Sat, 25-Apr-87 19:36:42 EST References: <3290@burdvax.PRC.Unisys.COM> <1722@plus5.UUCP> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 70 Summary: In article <1722@plus5.UUCP> hokey@plus5.UUCP (Hokey) writes: >There is no current way to perform compile-time initailization of unions. > >If the braindead compile-time first-member initialization rule in the proposed >C standard gets accepted, you will have a way to perform compile-time >initialization of unions which only need to be initialized to the type of >their initial members. In my opinion, this mechanism is *worse* than not >having *any* way to perform compile-time initialization of unions. >-- >Hokey Darn Tootin, Hokey. What's the &&#%!@%? point of initializing a union (actually, making an initialized thing use unions ) if you can't use the union feature to initialize different parts differently? When a structure is initialized, {}'s are placed around the initializers for its members ( they aren't always required, but I try not to know that ). When a union is initialized, and the default 'first member' is used, there are presumably (no ANSI draft HERE...) no {}'s required, since there is only one 'thing' inside a union initializer ( the thing often requiring {}'s itself). Is this the case? Actually I seem to remember that ANSI allows redundant {}'s around scalar initializers and around other {}'s, and therefore around union initializers too. How about this: union { double dvar; int ivar[2]; struct svar{ char flob[2]; char *(*func)(); } } unarray[4] = { /* this { is for the array */ .dvar = 12.00, /* This entire line (without the comma) is the initializer for unarray[0]. Since that is a union object, the initializer is . = where is a valid initializer for an object of the same type as the selected member, in this case double */ .dvar = 13.100, .ivar = { 1,0 }, /* the {}'s are for ivar, an array of ints */ .svar = { "?", malloc } /* the {}'s are for svar, a struct */ }; /* end the array unarray */ If no '. tag =' is given, the first member of the union is used. So what d'y'all think? The '.' I think may be useful to allow lexers to treat the identifier differently if they want to (once defined, union and struct tags appear only after -> and . in C, and since they are in a separate name space, this fact may be relied upon by some parsing methods ). The device only appears when the compiler 'knows' that a union is being initialized. However YACC doesn't know that (barring some strange mechanism to tell it). So it is important that such a thing be parseable in a context-free way. I believe this thing is, since '.' can never appear at the start of a normal expression. ( the token '.', not the character ). If directly nested unions are used, you can end up with things like .member = .deeper_member = { 2, "blat" } It is tempting to suggest that .member.deeper_member = ... be allowed, which would be far more readable. This seems like rampantly feeping creaturism, especially as directly nested unions are so scarcely used. I guess it's too late to get anything done about this, right? -- ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg Have vAX, will hack...