[英]what does typedef struct node *NODE indicate?
struct node
{
int coef;
int exp;
struct node *link;
};
typedef struct node *NODE;
它将NODE
定义为struct node *
类型的同义词,所以当你声明一个NODE
类型的变量时,你实际上是在声明一个指向struct node
的指针。
就个人而言,我认为这样的声明不是一个好主意:您“隐藏了一个指针”(这几乎总是一个坏主意),而且,您没有以任何方式在新名称中强调这一事实.
它使NODE
成为struct node *
的 typedef。
NODE
成为struct node*
的别名。
编辑:好的,对于评论(如果我将我的答案写为评论,它将太长且未格式化):
没有什么不同的写法。 在这里, typedef
仅用于为指向struct node
指针创建同义词/别名。
使用示例是:
void f()
{
// some code
NODE p = NULL;
// do something with p
// for example use malloc and so on
// like: p = malloc( sizeof( struct node ) );
// and access like: p->coef = ..; p->expr = ..
// do something with p and free the memory later (if malloc is used)
}
是相同的
void f()
{
// some code
struct node* p = NULL;
// do something with p
}
使用NODE
使它更短(无论如何,我不会建议这样的typedef
,因为您正在隐藏,它是一个指针,而不是struct
或其他类型,如@Matteo Italia 的回答中所述)。
您所指的格式是:“typedef struct{}type_name 格式”是另一回事。 这是C
的一种技巧,以避免编写struct
关键字(因为它在C
是必需的,而不是在C++
)。 所以
typedef struct node
{
//..
} NODE;
将为struct node
制作NODE
别名。 所以,与上面相同的例子:
void f()
{
// some code
NODE p;
// do something with p
// note that now p is automatically allocated, it's real struct
// not a pointer. So you can access its members like:
// p.coef or p.expr, etc.
}
是相同的
void f()
{
// some code
struct node p;
// do something with p
}
请注意,现在p
不是指针,而是struct node
。
只是告诉你可以每次只使用 NODE 创建节点类型的指针,而不是每次都写 struct node *
typedef struct node *NODE
表示什么?
大写不好
为MACROS保留所有大写标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.