Need help with union trick: summary of replies

Chris Torek chris at mimsy.umd.edu
Mon Jan 7 04:16:48 AEST 1991


In at least two articles at least two people write about

>ANSI anonymous structures

but X3.159-1989 does not have what these people mean.

The idea, which is supported by a number of `C-like' compilers, is to
allow union (and structure) definitions of the form

	struct value {
		enum { integer, floating } v_kind;
		union {
			int	v_integer;	/* value if integer */
			float	v_floating;	/* value if floating */
		};
	} v;

after which references of the form `v.v_integer' would be allowed.
Although this is useful, it is not part of `ANSI C'.  Some people use
workarounds that look like:

	struct value {
		enum { integer, floating } v_kind;
		union {
			int	vu_integer;
			float	vu_floating;
		} v_un;
	#define	v_integer	v_un.vu_integer
	#define	v_floating	v_un.vu_floating
	} v;

which causes a reference of the form `v.v_integer' to expand to one
of the form `v.v_un.vu_integer'.  Other people type in the extra `un.vu_'
every time (to emphasize the union).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list