Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C ­function declaration in a parameter list

Tags:

c++

c

function

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++?

like image 898
marmalmad Avatar asked Apr 06 '15 11:04

marmalmad


1 Answers

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))
like image 54
fredoverflow Avatar answered Sep 19 '22 09:09

fredoverflow