用C指针语法:为什么*只适用于第一个变量? [英] Pointer syntax in C: why does * only apply to the first variable?
问题描述
在C中的声明如下:
int* a, b;
将宣布 A
类型为int *
和 B
类型 INT
。我很清楚这个陷阱,但我想知道的是的为什么的是这样工作的。为什么不是也声明 B
为为int *
,因为大多数人会直觉地想到?换句话说,为什么 *
适用于变量名,而不是类型?
will declare a
as type int*
and b
as type int
. I'm well aware of this trap, but what I want to know is why it works this way. Why doesn't it also declare b
as int*
, as most people would intuitively expect? In other words, why does *
apply to the variable name, rather than the type?
当然,你可以这样写它是与它如何的更一致的实际的作品:
Sure you could write it this way to be more consistent with how it actually works:
int *a, b;
不过,我和大家跟我谈过的思考方面的一个类型是指向int的指针的,而不是的 a是一个指向一些数据和类型这些数据是int的
对您只需通过C的设计师一个错误的决定或者是有它的原因的分析这样一些很好的理由吗?我敢肯定,这个问题已经回答过了,但我似乎无法用搜索来找到它。
Was this simply a bad decision by the designers of C or is there some good reason why it's parsed this way? I'm sure the question has been answered before, but I can't seem to find it using the search.
推荐答案
有上的 c语言,说的发展,这些声明的语法反映了观察我,* PI和PPI **在离$ p $使用时都产生一个int类型pssion。搜索这句话在页面上找到相关的部分,讨论这个问题。
There's a web page on The Development of the C Language that says, "The syntax of these declarations reflects the observation that i, *pi, and **ppi all yield an int type when used in an expression." Search for that sentence on the page to find the relevant section that talks about this question.
这篇关于用C指针语法:为什么*只适用于第一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!