为什么指针声明中的星号特定于标识符而不是数据类型? [英] Why is the asterisk in a pointer declaration specific to the identifier and not the data type?
问题描述
愚蠢的问题,但尝试掌握潜在的机制/哲学以巩固我的理解.
Silly question, but trying to grasp the underlying mechanics/philosophy to solidify my understanding.
int myInt; // declares a variable of type integer, named myInt. Intuitive.
int* myPtr; // declares a variable of type pointer-to-integer. Also intuitive.
int myInt2, myInt3; // two more integer variables.. yay!! This makes sense.
// so the pattern is [type] [identifier] <,more-identifiers>;
int* myInt4, myInt5; // an int pointer then an integer. Brain hurts!
推荐答案
TL; DR-这就是C语法设计的工作方式.
TL;DR - that is how C syntax is designed to work.
*
被认为是附加变量,因为它定义了变量的类型,而不是 datatype .对象的类型应该是对象本身的属性,因此考虑该属性的 identifier 与该对象(变量)相关是很有意义的.
The *
is considered attached to the variable, because, it defines the type of the variable, not the datatype. The type of an object is supposed to be a property of the object itself, so it makes sense to consider the identifier of the property is associated with the object(variable).
为澄清起见,我们说int * p;
的意思是说p
是 pointer 类型的变量,它指向int
.因此,将*
附加到变量而不是数据类型是有道理的.
To clarify, by saying int * p;
, we mean to say, p
is a variable of type pointer which points to an int
. So, it makes sense to attach the *
to the variable, rather than to the datatype.
这篇关于为什么指针声明中的星号特定于标识符而不是数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!