How to initialize unions

Chris Torek chris at mimsy.UUCP
Wed Apr 5 08:49:01 AEST 1989


In article <3742 at phri.UUCP> roy at phri.UUCP (Roy Smith) writes:
>I've got a structure which looks like:
>
>	struct ident {
>	        char *name;
>	        int type;
>	        union {
>	                double f;
>	                int i;
>	        } value;
>	};
>
>and I want to initialize the name, type, and value.i elements of an array
>of them them at compile time.

Unless you have an extension, you cannot do this unless you change the
order to

		union {
			int i;
			double f;
		} value;

Even then, you must have a pANS-conformant compiler or its equivalent;
most PCCs will refuse to initialise any unions, giving the error message

>"operands of = have incompatible types"

GCC should do the job, with the re-ordered union.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list