Function pointer as an argument
Let say you have function
int func(int a, float b);
So pointer to it will be
int (*func_pointer)(int, float);
So than you could use it like this
func_pointer = func;
(*func_pointer)(1, 1.0);
/*below also works*/
func_pointer(1, 1.0);
To avoid specifying full pointer type every time you need it you coud typedef
it
typedef int (*FUNC_PTR)(int, float);
and than use like any other type
void executor(FUNC_PTR func)
{
func(1, 1.0);
}
int silly_func(int a, float b)
{
//do some stuff
}
main()
{
FUNC_PTR ptr;
ptr = silly_func;
executor(ptr);
/* this should also wotk */
executor(silly_func)
}
I suggest looking at the world-famous C faqs.
Definitely.
void f(void (*a)()) {
a();
}
void test() {
printf("hello world\n");
}
int main() {
f(&test);
return 0;
}