Save to assign union member to each other ?

Michael Henning michi at ptcburp.ptcbu.oz.au
Thu Feb 7 09:44:45 AEST 1991


Something I could not figure out from the various references...

Given

union {
	int	i;
	double	d;
} u;

is it legal to assign one member of the union to another?  Examples:

	u.i = !u.d;
	u.d = (double)u.i;
	u.d = (double)(u.i * 3.14);

The various standard references (K&R, Harbison & Steele) are not clear
about this. Obviously, the address of the lvalue on the left may be the
same as the address of one or more operands in the expression on the right,
as in

	x = -x * x;

But what about the case where the union contains objects of different types
and sizes, as above? It looks legal to me. Has anyone ever encountered a
compiler that generates incorrect code for such a case?

							Michi.

-- 
      -m------- Michael Henning			+61 75 950255
    ---mmm----- Pyramid Technology		+61 75 522475 FAX
  -----mmmmm--- Research Park, Bond University	michi at ptcburp.ptcbu.oz.au
-------mmmmmmm- Gold Coast, Q 4229, AUSTRALIA	uunet!munnari!ptcburp.oz!michi



More information about the Comp.lang.c mailing list