为什么指针声明中的星号特定于标识符而不是数据类型?

ade*_*gle 3 c syntax types pointers

愚蠢的问题,但试图掌握潜在的机制/哲学以巩固我的理解。

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!
Run Code Online (Sandbox Code Playgroud)

Sou*_*osh 5

TL;DR - 这就是 C 语法的设计原理。

*视为附加到变量,因为它定义了变量的类型,而不是数据类型。对象的类型应该是对象本身的属性,因此考虑属性的标识符与对象(变量)相关联是有意义的。

为了澄清,通过说int * p;,我们的意思是说,是一个指向 的指针p类型的变量。因此,将 附加到变量而不是数据类型是有意义的。int*