Initializing unions, here and now.

38613457 38613457 at sdcc3.UUCP
Wed Oct 31 14:32:56 AEST 1984


fig()

I here a lot about initializing unions, this is a pointer to
a union, but the space it points to is big enough for aything
that you should be able to put into it.  Good, Bad, any better
in the here now where I can play with them?  As to ANSI, I
would be happy if the compiler would cram whatever I choose
to initialize into a space big enough to use for any of the
members and position it for the type that it is.  If the requires
specific typecasting then ok, but here and now does anyone have
something which looks better.

				Jim sdcsvax!sdcc6!ix269
				    {the 38613457 should be
					-$185 by oct 31 )

--------------------- mangle this line -------------------
#include <stdio.h>

union Val {
    int  oi;
    char oc;
    double od;
};

typedef double BIG;

tmpchar[sizeof(BIG)] = 'A';

struct Onion {
  int valtyp;
  union Val *val;
} onion[] = {
    1,	(union Val *) tmpchar
};

main()
{
extern Onion onion[];
    printf("1, val = %c\n",onion->val->oc);
    onion->val->od = 3.1415927;
    printf("2, val = %lf\n",onion->val->od);
    onion->val->oi = 1;
    printf("3, val = %d\n",onion->val->oi);
}
/* eot */



More information about the Comp.lang.c mailing list