Standard for union initializatio

Kevin Martin kpmartin at watmath.UUCP
Sun Jan 27 09:52:04 AEST 1985


>>I saw a suggestion in net.lang.c a few months ago that I found very sensible,
>>readable and hygienic:
>>
>>        union   {
>>                int i;
>>                float x;
>>                char *str;
>>        } ixstr = { , 3.14, };
>>
>>if you want it to start off as a float, etc.
>>
>>Note that:
>>1. it is (almost) isomorphous to a struct initialization,
>>2. it also works nicely for arrays of unions.
>
>Now if the committee continues to stick with the first member
>rule despite intelligent ideas like this, we should petition ANSI
>for a new committee.

"Intelligent idea"??? This is just another accident waiting to happen!
This looks ok for a nice short example like this, but frequently, the
union's definition and initialization are far apart (and maybe in different
source files). This makes it easy to add another union element, and
inadvertantly screw up the initializers royally without as much as a
peep from the compiler.

The first-element rule is a reasonable default, and there have been far
better suggestions than the above to select another element.
                    Kevin Martin, UofW Software Development Group.



More information about the Comp.lang.c mailing list