[comp.lang.c] Generic function pointers

karl@haddock.ima.isc.com (Karl Heuer) (10/19/90)

In article <9106@b11.ingr.com> polfer@b11.ingr.com (? Polfer) writes:
>Under ANSI, what is the proper form for the definition of a void pointer to a
>function ...

Your terminology is confusing.  What you want is a generic function-pointer
type, which would be analogous to the way that "void *" is a generic data-
pointer type.

The answer is that *any* function-pointer type can be converted to any other
(as long as you convert it back to the correct type before invoking it).  You
might as well cast to (void (*)(void)), that being the simplest.  It's
probably a good idea to use a typedef like "generic_function_t" to distinguish
it from an actual void-to-void function.

>[How about (void (*)()) ?]  does the absence of "void" in the parameter lists
>make a difference in a declaration and/or definition of such a pointer?

That's the old-style (non-prototyped) syntax, which is obsolescent.  If you
know you're only going to be dealing with ANSI compilers, don't use that form.
If you need backward portability, use
	#if defined(__STDC__)
	typedef void (*generic_function_t)(void);
	#else
	typedef void (*generic_function_t)();
	#endif

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