How do I prototype this?

Karl Heuer karl at haddock.ima.isc.com
Sun Jul 1 05:14:59 AEST 1990


In article <326 at demott.COM> kdq at demott.COM (Kevin D. Quitt) writes:
>I'm invoking functions through an array of struct.  My compiler generates
>a warning in the following two cases.  How do I declare prototypes given:
>	(*configuration[ tnum ].fn)( cfg, token );

Change the struct definition:
	struct token_def { ... i16 (*fn)(cfg_t, token_t); }
where cfg_t and token_t are the types of the arguments.

>	struct  parse_info { CMD_IN  *pici; ... }
>	(*(pi[0].pici->func))();

Similarly, use a prototype for the `func' member of the struct you've
typedef'd to `CMD_IN'.

>    Note also that depending upon the circumstances, the call will have
>zero, one or two parameters.

Oh, that makes it a bit trickier.  I presume the individual functions are not
variadic (since you want to be able to pass zero args), and you just want to
be able to store them all in the same structure type.  Okay, then, the proper
way to do it is to cast the constants to a common type in the initializer, and
cast them back to the correct type before using them:
	struct token_def { ... void (*fn)(void); };
	struct token_def configuration[] = {
	    { TITLES, 0, "TITLES", (void (*)(void))paint_titles },
	};
	(*(i16 (*)(cfg_t, token_t))configuration[ tnum ].fn)( cfg, token );

Any function-pointer type may be used for the generic pointer; I used the
simplest, `void (*)(void)'.  You may want to use a typedef for this and the
other function-pointer types.

Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint



More information about the Comp.lang.c mailing list