Joh*_*ode 31

读取毛茸茸的声明符的一般过程是找到最左边的标识符并解决问题,记住它[]()在之前绑定*(即,*a[]是一个指针数组,而不是指向数组的指针).由于参数列表中缺少标识符,这种情况变得更加困难,但[]之前再次绑定*,因此我们知道这*[]表示一组poitners.

所以,给定

void (*func)(int(*[ ])());
Run Code Online (Sandbox Code Playgroud)

我们按如下方式细分:

       func                   -- func
      *func                   -- is a pointer
     (*func)(           )     -- to a function taking
     (*func)(     [ ]   )     --    an array
     (*func)(    *[ ]   )     --    of pointers
     (*func)(   (*[ ])())     --    to functions taking 
                              --      an unspecified number of parameters
     (*func)(int(*[ ])())     --    returning int
void (*func)(int(*[ ])());    -- and returning void
Run Code Online (Sandbox Code Playgroud)

这在实践中会是什么样子,如下所示:

/**
 * Define the functions that will be part of the function array
 */
int foo() { int i; ...; return i; }
int bar() { int j; ...; return j; }
int baz() { int k; ...; return k; }
/**
 * Define a function that takes the array of pointers to functions
 */
void blurga(int (*fa[])())
{
  int i;
  int x;
  for (i = 0; fa[i] != NULL; i++)
  {
    x = fa[i](); /* or x = (*fa[i])(); */
    ...
  }
}    
...
/**
 * Declare and initialize an array of pointers to functions returning int
 */
int (*funcArray[])() = {foo, bar, baz, NULL};
/**
 * Declare our function pointer
 */
void (*func)(int(*[ ])());
/**
 * Assign the function pointer
 */
func = blurga;
/**
 * Call the function "blurga" through the function pointer "func"
 */
func(funcArray); /* or (*func)(funcArray); */
Run Code Online (Sandbox Code Playgroud)


caf*_*caf 20

这不是声明,而是声明.

它声明func为一个指向函数的指针,该函数返回void并获取一个类型的参数int (*[])(),该参数本身是一个指向函数的指针,该函数返回int并获取固定但未指定数量的参数.


cdecl输出你的小信仰:

cdecl> explain void (*f)(int(*[ ])());
declare f as pointer to function (array of pointer to function returning int) returning void
Run Code Online (Sandbox Code Playgroud)

  • 在我回答之后,C++标签是由原始海报以外的人添加的.在我的书中,这一举动太聪明了. (3认同)

小智 5

是:

$ cdecl
explain void (* x)(int (*[])());
declare x as pointer to function 
  (array of pointer to function returning int) returning void