Passing types to a subroutine

Karl Heuer karl at haddock.ima.isc.com
Sat Nov 12 07:30:47 AEST 1988


In article <32119 at bbn.COM> mesard at bbn.com (Wayne Mesard) writes:
>Or, how 'bout using a good old union to get rid of the extra param:
>   union ptr { float *f; double *d; };
>   main() {
>       float f[10]; double d[10];
>       sub(0, f); sub(1, d);
>   }
>   void sub(t, p) enum whichtype t; union ptr p; { ... }

This is not correct code.  The second parameter is declared to be `union ptr',
but the type actually passed is one of its member types.  This will fail if,
for example, the implementation passes scalars on the stack and structs/unions
with some other mechanism.  To do this `right', you'd have to declare a union
object, copy the value `f' or `d' into its appropriate member, and then pass
the union as the actual argument.

(Also, since you've already gone to the trouble of declaring an enum, you
should probably say `TYPE_FLOAT' or `TYPE_DOUBLE' instead of `0' or `1'.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list