结构可以具有``卡*''类型吗?(示例书)

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

本文关键字:类型 结构      更新时间:2023-10-16

我的书说

typedef Card* Cardptr;

"将新类型名称Cardptr定义为类型Card*的同义词。"我看到*符号仅修改Cardptr,如果还有其他同义词,则不会通过*符号修改。这让我感到困惑。我的书使实际结构类型似乎是Card*,这使我认为其他同义词将具有

中的类型Card*
typedef Card* Cardptr, n;

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

typedef Card *Cardptr, n;

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

c 通常不关心whitespaces,因此编译器将以下两个语句视为相同:

typedef Card* Cardptr;
typedef Card *Cardptr;

同样,这三个声明

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

是无法区分的。


如果他们移动* [],那么您会知道该类型实际上是Card,而Cardptr只是一个指针,而n不是指针。这样做的原因是什么?

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

这会让我更喜欢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?

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