Path: utzoo!attcan!uunet!ncrlnk!ncrcae!ece-csc!mcnc!rutgers!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!sm.unisys.com!ism780c!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Passing types to a subroutine Message-ID: <10600@haddock.ima.isc.com> Date: 11 Nov 88 21:30:47 GMT References: <14461@mimsy.UUCP> <32119@bbn.COM> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 20 In article <32119@bbn.COM> mesard@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@haddock.isc.com), The Walking Lint