Path: utzoo!attcan!uunet!husc6!uwvax!umn-d-ub!nic.MR.NET!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Passing types to a subroutine Message-ID: <14485@mimsy.UUCP> Date: 11 Nov 88 02:09:39 GMT References: <14461@mimsy.UUCP> <32119@bbn.COM> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 44 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: You can do this, but not as coded. (Original code appears below.) Instead, you must augment main() with a `union ptr' variable, and change the calls to u.f = f; sub(0, u); u.p = d; sub(1, d); The code below is not type-correct and WILL NOT WORK on (eg) a Pyramid. I would also suggest using TYPE_FLOAT and TYPE_DOUBLE rather than the literal 0 and 1 constants. - union ptr { - float *f; - double *d; - }; - - enum whichtype {TYPE_FLOAT, TYPE_DOUBLE}; - - main() - { - void sub(); - float f[10]; - double d[10]; - - f[1] = 1.234; - d[1] = -5.432; - sub(0, f); - sub(1, d); - printf("Incidently, the sizeof the union is %d\n", sizeof(union ptr)); - } - - - void sub(t, p) - enum whichtype t; - union ptr p; [rest deleted] -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris