Path: utzoo!attcan!uunet!husc6!ukma!mailrus!cornell!uw-beaver!microsoft!clayj From: clayj@microsoft.UUCP (Clay Jackson) Newsgroups: comp.lang.c Subject: Re: Passing types to a subroutine Message-ID: <1221@microsoft.UUCP> Date: 11 Nov 88 00:35:46 GMT References: <14457@mimsy.UUCP> <14461@mimsy.UUCP> Reply-To: clayj@microsoft.UUCP (Clay Jackson) Organization: Microsoft Corp., Redmond WA Lines: 47 In article <14461@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >>need to do is something of the following: >[edited] >> subroutine(...., , ...); >> subroutine(...., , ...); >> >>and have the subroutine do the right thing with a[]. > >This is easy: you cannot do it at all in C. > >The best approximation is something like this: > (edited for brevity) > float a[100]; > subroutine(TYPE_FLOAT, a, (double *)NULL); > ... > > double a[100]; > subroutine(TYPE_DOUBLE, (float *)NULL, a); Another approach, which I have seen implemented in a commercial product (NOT made by Microsoft!) is to define a union, ala: union foo { long long_arg; short short_arg; double double_arg; }; Then, reference it in a struct struct myargs { union foo my_val; int arg_type }; Then you can even get fancier, and use defines to set up names, like #define LONG 0 #define SHORT 1 . . and so on, to figure out what the value of arg_type should be. I first saw this used in "Unify", a database product made by the Unify Corp. There is nothing, as far as I know, proprietary about it, and it seems like a pretty elegant solution for something that would otherwise be pretty hard to do in C.