C函数指针语法

前端之家收集整理的这篇文章主要介绍了C函数指针语法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的问题很简单.

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

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.

猜你在找的C&C++相关文章