I'm find out a bit confusing thing in C code
struct SomeStruct {
// ...
void (*f)(const void *x);
};
void do_some( void f(const void *x) ) { // what?
struct SomeStruct* v;
// ...
v->f = f;
}
As I can understand do_some
take function instead function pointer.
But what is difference with void do_some( void (*f)(const void *x) )
in practice? When I should use this? Is this allowed in C++?
There is no difference. It's just syntactic sugar. It is allowed in both C and C++.
Function parameters are simply rewritten by the compiler as function pointer parameters, just as array parameters are rewritten by the compiler as pointer parameters.
For reference, here is an excerpt from the C standard, section 3.7.1:
g(int (*funcp)(void))
// ...
or, equivalently,
g(int func(void))
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