Path: utzoo!utgpu!attcan!uunet!husc6!ukma!mailrus!bbn!bbn.com!mesard From: mesard@bbn.com (Wayne Mesard) Newsgroups: comp.lang.c Subject: Re: Passing types to a subroutine Message-ID: <32119@bbn.COM> Date: 10 Nov 88 15:17:54 GMT References: <14461@mimsy.UUCP> Sender: news@bbn.COM Lines: 64 From article <14461@mimsy.UUCP>, by chris@mimsy.UUCP (Chris Torek): > In article <14457@mimsy.UUCP> sjr@mimsy.UUCP (Stephen J. Roznowski) writes: >>I'm in the process of developing a piece of code that needs >>to be portable across several different machines. What I >>need to do is something of the following: > [edited] >> subroutine(...., , ...); >> subroutine(...., , ...); > > The best approximation is something like this: > > float a[100]; > subroutine(TYPE_FLOAT, a, (double *)NULL); > ... > > double a[100]; > subroutine(TYPE_DOUBLE, (float *)NULL, a); > ... > > subroutine(t, iffloat, ifdouble) > enum whichtype t; > float *iffloat; > double *ifdouble; [Rest deleted.] Or, how 'bout using a good old union to get rid of the extra param: 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; { switch(t) { case TYPE_DOUBLE: printf("Itsa double, cell 1 is %g\n", p.d[1]); break; case TYPE_FLOAT: printf("Itsa float, cell 1 is %g\n", p.f[1]); break; } } -- void *Wayne_Mesard(); MESARD@BBN.COM BBN, Cambridge, MA