C函数指针语法 [英] C function pointer syntax

查看:150
本文介绍了C函数指针语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单。

通常,声明一些变量时,你把它的类型之前,如:

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.

推荐答案

我在回答<解释这一个href=\"http://programmers.stackexchange.com/questions/117024/why-was-the-c-syntax-for-arrays-pointers-and-functions-designed-this-way/117039#117039\">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函数指针语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆