Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!uwvax!daffy!cat9.cs.wisc.edu!schaut From: schaut@cat9.cs.wisc.edu (Rick Schaut) Newsgroups: comp.lang.c Subject: Re: Arrays of functions - calling them with different number of args. Message-ID: <4410@daffy.cs.wisc.edu> Date: 7 Mar 90 21:09:44 GMT References: <8553@cbnewsh.ATT.COM> <22954@mimsy.umd.edu> Sender: news@daffy.cs.wisc.edu Distribution: na Organization: U of Wisconsin CS Dept Lines: 46 In article <22954@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes: | In article <8553@cbnewsh.ATT.COM> ijk@cbnewsh.ATT.COM (ihor.j.kinal) writes: | >Obviously, I could provide a list of arguments for each function | >in my table, and I could test if an arg two is present, then I call | > | > action_tbl [ action_ind ] ( one_arg, two_arg ) | > | >and otherwise, call the old way. This seems incredibly clumsy. | | (which is indeed rather clumsy) is not truly portable, not under ANSI C. | The reason is that `action_tbl' must have one of the two types | | sometype (*action_tbl[SIZE])(type1 arg1); | | or | | sometype (*action_tbl[SIZE])(type1 arg1, type2 arg2); | | It cannot have both, save by being a union. Fortunately, all pointers | to functions have the same underlying size, so you can get away with | using one of these types, and applying casts to force the other type | to match. That is: [One option for implementing this deleted.] If all you want is a way to prototype the functions, sometype (*action_tbl)[SIZE](type1 arg1,...); will do nicely. This does sacrifice type checking on the second argument, but I get the impression this is not an important consideration. | >What I really want is to construct an arg list and pass that to my | >function call, but I can't figure how to do that. | | There is no such option. It would be nice, but it does not exist. If the compiler is ANSI compliant, then there is, indeed, a portable and standard way to implement this. In fact, the ANSI version of printf is implemented using a variable-length argument list. For further reference, see K&R, 2nd Ed. Sect. 7.3 (page 155). -- Rick (schaut@garfield.cs.wisc.edu) "I'm a theory geek; we use Turing machines!"--Gary Lewandowski