C语言 C 函数指针语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14114749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C function pointer syntax
提问by user1941583
My question is a very simple one.
我的问题很简单。
Normally, when declaring some variable, you put its type before it, like:
通常,在声明某个变量时,将其类型放在它之前,例如:
int a;
a function pointer may have type like: int(*)(int,int), in case we point to a function that takes two integers and returns an integer. But, when declaring such a pointer, its identifier is not after the type, like:
一个函数指针可能有这样的类型:int(*)(int,int),以防我们指向一个接受两个整数并返回一个整数的函数。但是,在声明这样的指针时,它的标识符不在类型之后,例如:
int(*)(int,int) mypointer;
instead, you must write the identifier in the middle:
相反,您必须在中间写下标识符:
int(*mypointer)(int,int);
why is this so? Sorry, I know it's an embarrassingly easy question...
为什么会这样?对不起,我知道这是一个令人尴尬的简单问题......
Thanks to everybody for replying. A.S.
感谢大家的回复。作为
采纳答案by detly
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:
我在回答为什么数组、指针和函数的 C 语法是这样设计的?,它基本上归结为:
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 anint; if I write*arg[N]I'll have a float" rather than "funcmust be a pointer to a function taking thisand returning that".The C entry on Wikipediaclaims that:
Ritchie's idea was to declare identifiers in contexts resembling their use: "declaration reflects use".
...citing p122 of K&R2.
语言作者更喜欢使语法以变量为中心而不是以类型为中心。也就是说,他们在声明中需要一个程序员的外观,并认为“如果我写的表情
*func(arg),那将导致int;如果我写*arg[N]我有一个浮动”,而不是“func必须是一个指向函数服用此并返回那个”。Ritchie 的想法是在类似于其使用的上下文中声明标识符:“声明反映使用”。
...引用 K&R2 的 p122。
回答by atomicinf
This structure reflects how a normal function is declared (and used).
此结构反映了如何声明(和使用)普通函数。
Consider a normal function definition:
考虑一个正常的函数定义:
int foo (int bar, int baz, int quux);
Now consider defining a function pointer to a function of the same signature:
现在考虑定义一个指向相同签名函数的函数指针:
int (*foo) (int, int, int);
Notice how the two structures mirror each other? That makes *foomuch easier to identify as a function pointer rather than as something else.
注意这两个结构是如何相互反映的?这使得*foo更容易识别为函数指针而不是其他东西。
回答by Jerry Coffin
If you're dealing with a function (not a pointer to one), the name is in the middle too. It goes like: return-type function-name "(" argument-list ")" .... For example, in int foo(int), intis the return type, foothe name and intthe argument list.
如果您正在处理一个函数(不是指向某个函数的指针),则名称也在中间。它是这样的:return-type function-name "(" argument-list ")" ...。例如,在int foo(int),int是返回类型,foo名称和int参数列表。
A pointer to a function works pretty much the same way -- return type, then name, then argument list. In this case, we have to add a *to make it a pointer, and (since the *for a pointer is prefix) a pair of parentheses to bind the *to the name instead of the return type. For example, int *foo(int)would mean a function named foo that takes an int parameter and returns a pointer to an int. To get the * bound to fooinstead, we need parentheses, giving int (*foo)(int).
指向函数的指针的工作方式几乎相同——返回类型,然后是名称,然后是参数列表。在这种情况下,我们必须添加 a*使其成为指针,并且(因为*for a 指针是前缀)一对括号将 绑定*到名称而不是返回类型。例如,int *foo(int)表示一个名为 foo 的函数,它接受一个 int 参数并返回一个指向 int 的指针。为了让 * 绑定到foo,我们需要括号,给出 int (*foo)(int).
This gets particularly ugly when you need an array of pointers to functions. In such a case, most people find it easiest to use a typedef for the pointer type, then create an array of that type:
当您需要指向函数的指针数组时,这会变得特别难看。在这种情况下,大多数人发现将 typedef 用于指针类型最容易,然后创建该类型的数组:
typedef int (*fptr)(int);
fptr array[10];
回答by Vikas Kumar Sinha
I had seen at some places function pointers declared as
我在某些地方看到函数指针声明为
int (*foo) (int a, int b);
and at some places aand bare not mentioned and both still works.
并且在某些地方a并b没有被提及,但两者仍然有效。
so
所以
int (*foo) (int, int)
is also correct.
也是对的。
A very simple way that I found to remember is as mentioned below:
我发现记住的一种非常简单的方法如下所述:
Suppose function is declared as:
假设函数声明为:
int function (int a , int b);
Step1:Simply put function in parentheses:
步骤1:只需将函数放在括号中:
int (function) (int a , int b);
Step2:Place a *in front of function name and change the name:
Step2:*在函数名前加一个,改名:
int (*funcPntr) (int a , int b);
PS: I am not following proper coding guidelines for naming convention etc. in this answer.
PS:在这个答案中,我没有遵循命名约定等的正确编码指南。

