Passing Function Pointer
void (*f) (void)
means pointer to function with no arguments returning void.
void *(*f)(void *)
means pointer to function taking a void pointer and returning a void pointer.
Since the types are different, the compiler will not allow you to pass one to the other without casting. (Note that casting is not really the right answer here, and as @detly points out, results in undefined behavior.)
As for dereferencing pointers to functions, you don't have to explicitly put a "*" before a function pointer to call it. For instance, you could call your function pointer f just by doing
f();
A function pointer example
Say you have a function f
, which you'd like to pass to a function called takes_a_function
.
takes_a_function
will probably have a type like
void takes_a_function(void (*f)(void *data), void *data);
Notice how there are two arguments to takes_a_function
, a function pointer, and a void pointer to some data. Also note that the function f
happens to take a void pointer as an argument. The idea is that you can pass the data to takes_a_function
, and it will pass it along to f
. For example, takes_a_function
could be defined like
void takes_a_function(void (*f)(void *), void *data) {
f(data);
}
Now, let's write a function to pass to takes_a_function
. Our function will just print an int that is passed to it.
void prints_an_int(void *data) {
// The idiom for converting a void pointer to another kind
// of pointer. NO NEED TO CAST. Note this behavior is only
// defined if the pointer data really does point to an int.
int *i = data;
printf("%d", *i);
}
int i = 0;
takes_a_function(prints_an_int, &i);
A couple of key points about this example:
prints_an_int
has the same type as the function pointer expected bytakes_a_function
. No need to cast.- There's no need to use the
&
operator to create a reference to a function. This is why we can passprints_an_int
totakes_a_function
directly. But we could also saytakes_a_function(&prints_an_int, &i)
, and it would be the same. void*
basically means "pointer to unknown type." To actually do anything with it, you must assign a variable of typevoid*
to another pointer variable whose type you expect. This is only guaranteed to work if you actually pass in the correct pointer type! In this example, we can assigndata
to anint*
, since data really does point to an int. If you want more data than just an integer, a common pattern is to create your own struct type which includes all the fields you want, and pass that instead.- As a special case, the compiler does not require you to cast when assigning void pointers to other pointers and vice-versa. But again, you only get defined behavior if you eventually convert a void pointer back to the correct type.
For example you have a function
void * abc(void *)
{
//...
}
int ExecBlock (void (*f) (void), int isSet)
{
return ExecBlock2( abc , NULL, isSet); //use name of the function which have void * as parameter type list and return type void *
}
int ExecBlock2(void *(*f)(void *), void *arg, int isSet)
{
... more code
}
See function-pointers