Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!rice!sun-spots-request From: mikel@teraida.UUCP (Mikel Lechner) Newsgroups: comp.sys.sun Subject: Re: unions as function arguments Keywords: Software Message-ID: <2927@teraida.UUCP> Date: 21 Mar 89 10:04:01 GMT References: <892@fornax.UUCP> Sender: usenet@rice.edu Organization: Teradyne EDA Inc., Santa Clara, Calif. Lines: 28 Approved: Sun-Spots@rice.edu Original-Date: Wed, 8 Mar 89 10:17:53 PST X-Sun-Spots-Digest: Volume 7, Issue 203, message 3 of 14 X-Issue-Reference: v7n180 In article <892@fornax.UUCP> stevec@lccr.cs.sfu.ca (Steve Cumming) writes: > What I want to know is, is it legal to pass a union to a function? Yes, just like it is legal to pass a structure to a function. > In that case passing a constant 0 as a union should cause the compiler to > do the obvious thing. It don't. No. Passing an integer argument and passing a union containing an integer are not the same thing. This works on most systems because the union and integer are aligned the same, passed the same way, and are the same size. However, this is not strictly correct C. I ran into exactly this same problem with some code I inherited, when first porting to the Sun4. Sun took the liberty with the Sun4 to pass structs and unions differently from scalar arguments. The register window architecture of the SPARC is used to pass scalar arguments through registers. Non-scalar arguments are passed on the stack the old-fashioned way. If you pass an int to a function expecting a union. The caller puts the argument on the stack, and the receiving fuction looks for it on the stack. And the program usually gets a segmentation violation. Mikel Lechner decwrl!teraida!mikel Teradyne EDA (408) 980-5200 5155 Old Ironsides Drive Santa Clara, Ca 95054 [[ Isn't that what I said? --wnl ]]