Path: utzoo!attcan!uunet!ccicpg!felix!arcturus!evil From: evil@arcturus.UUCP (Wade Guthrie) Newsgroups: comp.lang.c Subject: Re: Passing types to a subroutine Summary: how about unions? Message-ID: <2553@arcturus> Date: 11 Nov 88 18:24:25 GMT References: <14457@mimsy.UUCP> <14461@mimsy.UUCP> Organization: Rockwell International, Anaheim, CA Lines: 52 In article <14461@mimsy.UUCP>, chris@mimsy.UUCP (Chris Torek) writes: > 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(...., , ...); > > > This is easy: you cannot do it at all in C. how about using unions -- something like union types { float *f; double *d; }; #define FLOAT 1 #define DOUBLE 2 subroutine(...,fred, type); union *fred; int type; { . . . switch(type) { case(FLOAT): . . . fred->f . . . break; case(DOUBLE): . . . fred->d . . . break; default: /* error */ } . . . } It's a little cumbersome, but it gets you there -- I used pointers to floats and doubles because, in most cases, they act like arrays. I've used things like this and it seems to work okay. Wade Guthrie Rockwell International Anaheim, CA (Rockwell doesn't necessarily believe / stand by what I'm saying; how could they when *I* don't even know what I'm talking about???)