dick@tjalk.UUCP (Dick Grune) (06/03/85)
Since the whole matter of initializing unions has been rekindled, I venture
to resubmit my letter of almost a year ago.
I saw a suggestion for the initialization of unions 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.
Dick Grune
Vrije Universiteit
de Boelelaan 1081
1081 HV Amsterdam
the Netherlands
hokey@plus5.UUCP (Hokey) (06/06/85)
In article <477@tjalk.UUCP> dick@tjalk.UUCP (Dick Grune) writes: >I saw a suggestion for the initialization of unions 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. I don't see this as sensible or readable (I'm not sure about hygienic, either). For starters, if one has a union of many elements, the initialization string is *huge*. For another, if elements are added, deleted, or reordered, the initialization string must be fixed. Thirdly, one has to look at the definition quite often when examining an array union initialization in order to keep everything in perspective. I still believe the best answer is to explicitly cast each initializer. It sure would be nice if the net could get some additional feedback from the Committee on this issue. -- Hokey ..ihnp4!plus5!hokey 314-725-9492