A normal function pointer would look like this:
void (*fun_ptr)(void);
However, I have seen this syntax used to cast something to a void function pointer:
(void (*)(void *))
As you can see it has no name and only has (*)
in place of a name. What does this mean? Is it only used for casting?
The syntax (void (*)(void *))
is a cast.
the destination type of the cast is a function pointer that takes a single parameter of type void *
and returns void
. An example of a function whose type matches this cast is:
void abc(void *param);
void (*)(void *)
is a type. It's conceptually similar to int *
. That doesn't have an identifier associated with it, either. void (*foo)(void *)
is a declaration of a variable foo
with that type.
A cast expression is of the form (<type>)<expr>
. So, (void (*)(void *))
can be a cast operator.
However, this form is not limited to cast expressions. It can be used wherever a type can. For example, as the type of an argument in a function prototype:
void bar(void (*)(void *));
That declares a function bar
which takes a function pointer as an argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With