void (*func)(int(*[ ])());
问问题
815 次
6 回答
31
读取毛茸茸的声明符的一般过程是找到最左边的标识符并找出路,记住它[]
并()
在之前绑定*
(即,*a[]
是指针数组,而不是指向数组的指针)。由于参数列表中缺少标识符,这种情况变得更加困难,但再次[]
绑定 before *
,因此我们知道这*[]
表示一个指针数组。
所以,给定
void (*func)(int(*[ ])());
我们将其分解如下:
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
这在实践中会是什么样子,如下所示:
/**
* 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); */
于 2009-11-06T15:39:49.633 回答
20
这不是声明,而是声明。
它声明func
为指向返回 void 并采用单个参数类型int (*[])()
的函数的指针,它本身是指向返回 int 并采用固定但未指定数量的参数的函数的指针。
cdecl 输出为 ye 的小信仰:
cdecl> explain void (*f)(int(*[ ])());
declare f as pointer to function (array of pointer to function returning int) returning void
于 2009-11-06T12:55:46.500 回答
5
是的:
$ cdecl 解释 void (* x)(int (*[])()); 将 x 声明为指向函数的指针 (指向函数返回 int 的指针数组)返回 void
于 2009-11-06T12:54:59.480 回答
2
于 2009-11-06T13:06:37.820 回答
2
Geordi
是一个 C++ 机器人,可以训练这个:
<litb> geordi: {} void (*func)(int(*[ ])());
<litb> geordi: -r << ETYPE_DESC(func)
<geordi> lvalue pointer to a function taking a pointer to a pointer to a nullary function
returning an integer and returning nothing
它可以做很多有用的事情,比如显示所有参数声明(实际上,这只是匹配原始C++
语法规则名称):
<litb> geordi: show parameter-declarations
<geordi> `int(*[ ])()`.
让我们从相反的方向进行:
<litb> geordi: {} int func;
<litb> geordi: make func a pointer to function returning void and taking array of pointer to
functions returning int
<litb> geordi: show
<geordi> {} void (* func)(int(*[])());
如果你问它,它会执行你给它的任何东西。如果你受过训练但忘记了一些可怕的括号规则,你也可以混合使用 C++ 和 geordi 风格的类型描述:
<litb> geordi: make func a (function returning void and taking (int(*)()) []) *
<geordi> {} void (* func)(int(*[])());
玩得开心!
于 2009-11-06T16:15:53.760 回答
2
void (*func)(blah);
是一个指向blah
作为参数的函数的指针,其中blah
它本身是int(*[ ])()
一个函数指针数组。
于 2009-11-06T12:59:13.973 回答