int*(*)(int*,int*(*)())的类型

Mou*_*hna 5 c variables types pointers declaration

int*(*)(int*,int*(*)())

我想知道它是什么类型的?,有人可以举例说明使用此类型的声明.

任何帮助都会很棒.

谢谢.

Mes*_*ssa 19

它是一个指向函数的指针,它返回int*并接受返回的函数int*指针int*(并接受未定义数量的参数;请参阅注释).

一些例子(看起来不太好,它只是构造成包含所提到的声明):

#include <stdio.h>

static int a = 10;
int* f1() {
    return &a;
}

static int b;
int* f2(int *j, int*(*f)()) {
    b = *j + *f();
    // this is just for demonstrational purpose, such usage
    // of global variable makes this function not thread-safe
    return &b;
} 


int main(int argc, char *argv[]) {
    int * (*ptr1)();
    int * (*ptr2) (int * , int * (*)());
    ptr1 = f1;
    ptr2 = f2;

    int i = 42;
    int *pi = ptr2(&i, ptr1);
    printf("%d\n", *pi);

    return 0;
}

// prints 52
Run Code Online (Sandbox Code Playgroud)

  • 严格来说,因为这是标记C,它接受未定义数量的参数.在C++中,空参数列表表示没有参数,在C中需要(void)来指定它.奥术但真实;-) (6认同)

Tim*_*fer 7

cdecl 是你的朋友:

$ cdecl explain 'int * (*x) (int * , int * (*)())'
declare x as pointer to function (pointer to int, pointer to function returning pointer to int) returning pointer to int
Run Code Online (Sandbox Code Playgroud)

  • 是的,除非它没有你添加的`x`没有工作,除非你首先理解语句你不会知道添加`x`;) (11认同)
  • 如果没有标识符(应该很容易发现),那么您可以通过在其前面添加“(”前缀并添加“)x”后缀来获取“cdecl”来解释它。然后它会说“`cast x into ...`”,后面是类型的解释。 (2认同)