Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site sfjec.UUCP Path: utzoo!watmath!clyde!floyd!harpo!eagle!mh3bs!sfjec!jss From: jss@sfjec.UUCP (J.S.Schwarz) Newsgroups: net.lang.c Subject: Re: Functions with union parameters Message-ID: <120@sfjec.UUCP> Date: Thu, 5-Apr-84 17:19:50 EST Article-I.D.: sfjec.120 Posted: Thu Apr 5 17:19:50 1984 Date-Received: Sat, 7-Apr-84 04:07:12 EST References: <4107@edai.UUCP> Organization: Bell Labs, Murray Hill Lines: 34 To recap: The question concerns a function with an argument declared to be union { ushort ch; char *p; } edai!ok claims that this is not portable. But what is nonportable in his(her?) examples is the suggested calls. The portable way to call a function with such an argument is to declare a variable in the caller of the same type and use it. Thus: typedef union { ushort ch ; char *p; } UNION; callee( arg ) UNION arg ; { ... } caller() { UNION v ; v.ch = ... ; callee( v ) ; v.p = ... ; callee( v ) ; .... } This is fully portable provided your C compiler supports struct and union arguments. (I realize that some don't.) This points out the absence in C of any way to take a value and turn it into a union except by assigning it to a variable of union type. Edai!ok also asks if there is any guarantee that members of a union all begin at offset 0. K&R 8.5 says: "A union may be thought of as a structure all of whose members begin at offset 0, ..." Jerry Schwarz -- BTL, Summit -- sfjec!jss P.S.: Since edai!ok hasn't seen the calls in the SIII kernel, I think it is unreasonable to accuse the authors of writing non portable code.