结构可以具有“Card*”类型吗? (书中的例子)

Can structures have the type `Card*`? (Example from book)

我的书说

typedef Card* Cardptr;

"defines the new type name Cardptr as a synonym for type Card*." 我看到 * 符号只修饰 Cardptr 而其他同义词(如果有的话)不会被 * 符号修饰。这让我很困惑。我的书使实际结构类型看起来像是 Card*,这让我认为其他同义词的类型应该是 Card*,如

typedef Card* Cardptr, n;

其中 n 也将具有类型 Card*。如果他们把 * 符号像这样移动会不会更清楚?

typedef Card *Cardptr, n;

这样,您就会知道类型实际上是 CardCardptr 只是指向它的指针,而 n 不是指针。这是什么原因?

C++ 通常不关心空格,因此编译器认为以下两个语句相同:

typedef Card* Cardptr;
typedef Card *Cardptr;

同理,三个声明

int* a;
int *a;
int * a;

无法区分。


Wouldn't it be clearer if they moved * [so] you would know that the type is actually Card, that Cardptr is simply a pointer to it, and that n is not a pointer. What is the reason for this?

编码人员编写声明的方式只是品味和风格的问题,两者都同样合理:

这会让我更喜欢 int *a 而不是 int* a:

int* a,b; // declares and int* and an int; illogical, right?

这会让我更喜欢 int* a 而不是 int *a:

int *a = 0; // a (an int*) is set to zero (a.k.a NULL), not *a; illogical, right?

现在,我的建议是:在这两种形式之间进行选择并始终如一地坚持下去。