C函数指针语法

编程入门 行业动态 更新时间:2024-10-27 16:26:53
本文介绍了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.

推荐答案

我在回答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函数指针语法

本文发布于:2023-07-30 00:18:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   语法   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!