凌乱的函数指针间pretation [英] Messy function pointer interpretation
问题描述
我碰巧遇到下面的函数指针。
I happen to come across the following function pointer.
char (*(*x())[])();
它看起来像以下格式的函数指针数组,但我看不出有什么的F - >(* X())的意思。如何跨preT这混乱的函数指针?
It looks like an array of function pointer in the following format, but I can't see what f -> (*x()) means. How to interpret this messy function pointer?
char (*f[])();
ADDED
随着约翰·博德的帮助下,我做出了榜样如下:
ADDED
With John Bode's help, I make an example as follows.
#include <stdio.h>
char foo() { return 'a'; }
char bar() { return 'b'; }
char blurga() { return 'c'; }
char bletch() { return 'd'; }
char (*gfunclist[])() = {foo, bar, blurga, bletch};
char (*(*x())[])()
{
static char (*funclist[4])() = {foo, bar, blurga, bletch};
return &funclist;
}
int main()
{
printf("%c\n",gfunclist[0]());
char (*(*fs)[4])();
fs = x();
printf("%c\n",(*fs)[1]());
}
我能得到预期的结果。
I could get the expected result.
smcho@prosseek temp2> ./a.out
a
b
和,你可以找到一个更好的实施<一个href=\"http://stackoverflow.com/questions/4107934/messy-function-pointer-how-to-remove-the-warning\">here.
And, you can find a better implementation here.
推荐答案
我的一般程序是出来找在声明中最左边的标识符,然后工作,我的方式,记住, []
和()
绑定之前 *
(即 * F()
通常被解析为 *(F())
和 *一个[]
正常解析为 *(A [])
)。
My general procedure is to find the leftmost identifier in the declaration, and then work my way out, remembering that []
and ()
bind before *
(i.e., *f()
is normally parsed as *(f())
and *a[]
is normally parsed as *(a[])
).
因此,
x -- x
x() -- is a function
*x() -- returning a pointer
(*x())[] -- to an array
*(*x())[] -- of pointers
(*(*x())[])() -- to functions
char (*(*x())[])(); -- returning char
将这样的野兽会是什么样的做法?
What would such a beast look like in practice?
char foo() { return 'a'; }
char bar() { return 'b'; }
char blurga() { return 'c'; }
char bletch() { return 'd'; }
/**
* funclist -- funclist
* funclist[] -- is an array
* *funclist[] -- of pointers
* (*funclist[])() -- to functions
* char (*funclist[])() -- returning char
*/
char (*funclist[])() = {foo, bar, blurga, bletch};
这位前pression &放大器; funclist
将返回一个指针数组,所以
The expression &funclist
will return a pointer to the array, so
char (*(*x())[])()
{
return &funclist;
}
这篇关于凌乱的函数指针间pretation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!