Xref: utzoo comp.lang.c:35860 comp.std.c:4252 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!munnari.oz.au!brolga!bunyip.cc.uq.oz.au!lance!ptcburp!michi From: michi@ptcburp.ptcbu.oz.au (Michael Henning) Newsgroups: comp.lang.c,comp.std.c Subject: Save to assign union member to each other ? Keywords: union, assignment Message-ID: <315@ptcburp.ptcbu.oz.au> Date: 6 Feb 91 22:44:45 GMT Followup-To: comp.lang.c Organization: Pyramid Technology Corporation Lines: 34 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@ptcburp.ptcbu.oz.au -------mmmmmmm- Gold Coast, Q 4229, AUSTRALIA uunet!munnari!ptcburp.oz!michi