【发布时间】:2016-01-13 04:11:37
【问题描述】:
愚蠢的问题,但试图掌握基本的机制/哲学来巩固我的理解。
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!
【问题讨论】:
-
你的大脑很痛,因为你坚持使用与语言不一致的风格。
-
应该是
int *myPtr;和int *myInt4, myInt5;stackoverflow.com/a/398414/125507 -
我应该注意到我已经用 C++ 编码了一段时间,最后偶然发现了这种语言的怪异之处。我总是将星号左对齐,因为我认为指针是类型的一部分。它是!您可以将
int*传递到模板中,int*可以是某事的类型。唯一不适用的地方,afaik,是在变量声明器中,其中指针是标识符的一部分!即使这样,如果您只声明一个变量,它也是不可见的。我知道这是因为 C 是这样工作的,但我认为有些伤脑筋是有道理的。