Passing functions in C
Gordon Burditt
gordon at sneaky.UUCP
Thu Mar 15 16:40:14 AEST 1990
>>i would like to be able to write a procedure that takes as
>>a parameter a procedure name and then using that name
>>calls the procedure.
>>
>>ie runAProcedure(myProcedure)
>> {
>> myProcedure
>>}
>>
>>Is this possible to do in C? Any help or advise is appreciated
>>tho please e-mail responses so the net won't be cluttered.
>
>NO! It is not possible to do in C! (Im pretty sure you cant do this
>in ANSI C either).
Yes, it is. Some really old compilers did accept myfunct(), where
myfunct is of type pointer-to-function-returning-something, as meaning
the same as (*myfunct)(). Modern compilers tend to at least give
warnings about it.
>From section 3.7.1 of the Jan 1988 ANSI C draft (sorry, that's the most
recent one I have):
QUOTE
To pass one function to another, one might say
int f(void)
/* ... */
g(f);
Note that f must be declared explicitly in the calling function, as
its appearance in the expression g(f) was not followed by (.
Then the definition of g might read
g(int (*funcp)(void))
{
/* ... */ (*funcp)() /* or funcp() ... */
}
or, equivalently,
g(int func(void))
{
/* ... */ func() /* or (*func)() ... */
}
END QUOTE.
ANSI allows this, and even gives an example of it.
Gordon L. Burditt
sneaky.lonestar.org!gordon
More information about the Comp.lang.c
mailing list