如何定义一个指向函数的指针,该函数返回一个指向函数的指针

bit*_*ore 5 c pointers declaration

如何定义返回函数指针的函数指针?

typedef int(*a)(int,int);
a (*b)(int,int);
Run Code Online (Sandbox Code Playgroud)

为什么这可以工作,但以下不能工作?

(int(*a)(int,int) ) (*b)(int,int);
Run Code Online (Sandbox Code Playgroud)

或者

int(*)(int,int) (*b)(int,int);
Run Code Online (Sandbox Code Playgroud)

或者

( int(*)(int,int) ) (*b)(int,int);
Run Code Online (Sandbox Code Playgroud)

Jus*_*eel 4

这是正确的方法:

int (*(*b)(int,int))(int,int);
Run Code Online (Sandbox Code Playgroud)

您可以编译以下代码来演示如何使用这两种方法。为了清楚起见,我个人会使用 typedef 方法。

#include <stdio.h>

int addition(int x,int y)
{
    return x + y;
}

int (*test(int x, int y))(int,int)
{
    return &addition;
}

typedef int (*a)(int, int);

int main()
{
    a (*b)(int,int);
    int (*(*c)(int,int))(int,int);
    b = &test;
    c = &test;
    printf(b == c ? "They are equal!\n" : "They are not equal :(\n");
}
Run Code Online (Sandbox Code Playgroud)