Arrays of functions - calling them with different number of args.

Rick Schaut schaut at cat9.cs.wisc.edu
Thu Mar 8 07:09:44 AEST 1990


In article <22954 at mimsy.umd.edu> chris at mimsy.umd.edu (Chris Torek) writes:
| In article <8553 at cbnewsh.ATT.COM> ijk at 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 at garfield.cs.wisc.edu)

"I'm a theory geek; we use Turing machines!"--Gary Lewandowski



More information about the Comp.lang.c mailing list