异常函数指针参数语法

时间:2013-12-22 01:37:38

标签: c syntax function-pointers

这两种语法有什么区别吗?

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

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

我一直在使用第一种形式,但我刚看到第二种形式,它似乎表现完全一样,而语法更清晰。

2 个答案:

答案 0 :(得分:1)

从根本上说,两种形式都没有区别。对于两者,你有一个函数指针作为参数。函数指针必须指向具有此原型的函数:void foo(void);

以下是使用示例:

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;
}

答案 1 :(得分:1)

没有区别。在这两种情况下,funptr都有类型void (*)(),一个函数指针类型。

C99标准,第6.7.5.3节,第8段:

  

参数声明为''函数返回类型''应为   调整为''指向函数返回类型的指针'',如6.3.2.1。