Functions with union parameters
J.S.Schwarz
jss at sfjec.UUCP
Fri Apr 6 08:19:50 AEST 1984
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.
More information about the Comp.lang.c
mailing list