简体   繁体   English

typedef struct node *NODE 表示什么?

[英]what does typedef struct node *NODE indicate?

struct node
{
    int coef;

    int exp;

    struct node *link;
};

typedef struct node *NODE;

It defines NODE as a synonym for the type struct node * , so when you'll be declaring a variable of type NODE you'll be actually declaring a pointer to struct node .它将NODE定义为struct node *类型的同义词,所以当你声明一个NODE类型的变量时,你实际上是在声明一个指向struct node的指针。

Personally, I don't think that such declaration is a good idea: you're "hiding a pointer" (which is almost always a bad idea), and, moreover, you are not highlighting this fact in any way into the new name.就个人而言,我认为这样的声明不是一个好主意:您“隐藏了一个指针”(这几乎总是一个坏主意),而且,您没有以任何方式在新名称中强调这一事实.

它使NODE成为struct node *的 typedef。

NODE becomes an alias for struct node* . NODE成为struct node*的别名。


EDIT: Okay, for the comment (if I write my answer as comment, it would be too long and not formatted):编辑:好的,对于评论(如果我将我的答案写为评论,它将太长且未格式化):

There's no different way to write this.没有什么不同的写法。 Here, typedef is used just to create a synonym/alias for pointer to struct node .在这里, typedef仅用于为指向struct node指针创建同义词/别名。
An example for usage would be:使用示例是:

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)
}

is the same as是相同的

void f()
{
    // some code
    struct node* p = NULL;
    // do something with p
}

Using NODE makes it just shorter (anyway, I wouldn't advise such typedef , as you're hiding, that it's a pointer , not a struct or other type, as mentioned in @Matteo Italia's answer).使用NODE使它更短(无论如何,我不会建议这样的typedef ,因为您正在隐藏,它是一个指针,而不是struct或其他类型,如@Matteo Italia 的回答中所述)。


The format, you're referring: "typedef struct{}type_name format" is something else.您所指的格式是:“typedef struct{}type_name 格式”是另一回事。 It's kind of a trick in C , to avoid writing struct keyword (as it's obligatory in C , and NOT in C++ ).这是C的一种技巧,以避免编写struct关键字(因为它在C是必需的,而不是在C++ )。 So所以

typedef struct node
{
    //..
} NODE;

would make NODE alias for struct node .将为struct node制作NODE别名。 So, the same example as above:所以,与上面相同的例子:

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.
}

is the same as是相同的

void f()
{
    // some code
    struct node p;
    // do something with p
}

NOTE that now, p is NOT a pointer, it's struct node .请注意,现在p不是指针,而是struct node

只是告诉你可以每次只使用 NODE 创建节点类型的指针,而不是每次都写 struct node *

what does typedef struct node *NODE indicate? typedef struct node *NODE表示什么?

UPPERCASE IS NO GOOD大写不好

Reserve ALL UPPERCASE identifiers for MACROS .MACROS保留所有大写标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM