user3126358
user3126358

Reputation: 511

unusual function pointer parameter syntax

is there any difference between this two syntaxes?

void fun( void (*funptr)() )
{
    funptr(); // calls the function
}

void fun( void funptr() )
{
    funptr(); // calls the function
}

i'd always been using the first form, but i've just seen the second one and it seems it behaves exactly the same, while the syntax is clearer.

Upvotes: 5

Views: 113

Answers (2)

Fiddling Bits
Fiddling Bits

Reputation: 8861

There is no difference, fundamentally, in either form. For both you have a function that takes a function pointer as its argument. The function pointer must point to a function with this prototype: void foo(void);.

Below is an example of use:

void fun1(void (*funptr)(void))
{
    funptr(); // calls the function
}

void fun2(void funptr(void))
{
    funptr(); // calls the function
}

void foo(void)
{
}

int main(void)
{ 
    void (*pFun)(void) = foo;
    fun1(pFun);
    fun2(pFun);

    return 0;
}

Upvotes: 1

newacct
newacct

Reputation: 122429

There is no difference. In both cases, funptr has type void (*)(), a function pointer type.

C99 standard, section 6.7.5.3, paragraph 8:

A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

Upvotes: 1

Related Questions