我的问题很简单。
通常,声明一些变量时,你把它的类型之前,如:
normally, when declaring some variable, you put its type before it, like:
int a;一个函数指针可能拥有一个类型,如:INT(*)(INT,INT),在情况下,我们指出,它有两个int和返回一个int的函数。但是,声明这样的指针时,它的标识符不是类型后,如:
a function pointer may have type like: int(*)(int,int), in case we point to a function that takes two int and returns an int. But, when declaring such a pointer, its identifier is not after the type, like:
int(*)(int,int) mypointer;相反,你必须写在中间的标识符:
instead, you must write the identifier in the middle:
int(*mypointer)(int,int);为什么会这样?对不起,我知道这是一个embarassingly简单的问题...
why is this so? Sorry, I know it's an embarassingly easy question...
感谢大家的回答。A.S。
Thanks to everybody for replying. A.S.
推荐答案我在回答Why是数组,指针,而这样设计的功能C语法,它基本上可以归结为:
I explain this in my answer to Why was the C syntax for arrays, pointers, and functions designed this way?, and it basically comes down to:
语言作者preferred使语法变量为中心,而不是类型为中心。也就是说,他们希望程序员看声明,并认为如果我写了前pression * FUNC(ARG),那将导致 INT ;如果我写 * ARG [N] 我将有一个浮动,而不是 FUNC 必须是指向一个功能采取的这个的和返回的是的。
the language authors preferred to make the syntax variable-centric rather than type-centric. That is, they wanted a programmer to look at the declaration and think "if I write the expression *func(arg), that'll result in an int; if I write *arg[N] I'll have a float" rather than "func must be a pointer to a function taking this and returning that".
这是维基百科的Ç进入声称:
里奇的想法是申报上下文识别类似的使用的声明反映使用
Ritchie's idea was to declare identifiers in contexts resembling their use: "declaration reflects use".
...援引K&放P122; R2
...citing p122 of K&R2.
更多推荐
C函数指针语法
发布评论