簡體   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;

它將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM