Path: utzoo!attcan!uunet!husc6!mailrus!cwjcc!hal!nic.MR.NET!tank!mimsy!sjr From: sjr@mimsy.UUCP (Stephen J. Roznowski) Newsgroups: comp.lang.c Subject: Re: Passing types to a subroutine Message-ID: <14549@mimsy.UUCP> Date: 14 Nov 88 23:18:54 GMT References: <14461@mimsy.UUCP> <32119@bbn.COM> <10600@haddock.ima.isc.com> Reply-To: sjr@mimsy.umd.edu.UUCP (Stephen J. Roznowski) Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 34 In article <10600@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes: >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. I have a problem with using unions. (actually two problems.) One is since I do not know the type until run time, I need to create a union that contains EVERY possible type. Now what happens if I want to pass an array that is inside of an array of structures? (for example) Second, it would not be unreasonable to have someone call this routine with a 10,000 element array of 128 bit double floating point values. I do not want to have to copy this data at any time, and total storage (in memory) needs to be kept as low as possible. [Of course these subroutines need to be as fast as possible] Stephen -- Stephen J. Roznowski sjr@mimsy.umd.edu