加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C函数指针语法

发布时间:2020-12-16 05:47:18 所属栏目:百科 来源:网络整理
导读:我的问题很简单. 通常,当声明一些变量时,您将其类型放在它之前,如: int a; 函数指针可能具有类型:int(*)(int,int),以防万一我们指向一个使用两个int并返回int的函数.但是,当声明这样的指针时,它的标识符不在类型之后,如: int(*)(int,int) mypointer; 相反
我的问题很简单.

通常,当声明一些变量时,您将其类型放在它之前,如:

int a;

函数指针可能具有类型:int(*)(int,int),以防万一我们指向一个使用两个int并返回int的函数.但是,当声明这样的指针时,它的标识符不在类型之后,如:

int(*)(int,int) mypointer;

相反,您必须在中间写入标识符:

int(*mypointer)(int,int);

为什么这样呢?
对不起,我知道这是一个令人尴尬的简单问题

感谢大家的回复.
如.

解决方法

我在 Why was the C syntax for arrays,pointers,and functions designed this way?的答复中解释一下,基本上归结为:

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“.

The 07001 claims that:

Ritchie’s idea was to declare identifiers in contexts resembling their use: “declaration reflects use”.

…citing p122 of K&R2.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读