What is the purpose of the first “node” in the declaration: “typedef struct node { - - - } Node;”?

前端 未结 6 1334
醉话见心
醉话见心 2020-12-30 08:06

I am studying code examples from my professor in order to become better acquainted with linked data structures.

In our linked-list.c example the professor defines a

6条回答
  •  轮回少年
    2020-12-30 08:58

    He is defining a temporary name for the node because he is using a well know technique to avoid writing struct node on the declaration of each struct object.

    If he would just do:

    struct node {
      int data;
      struct node *next;
    };
    

    you would have had to use:

    struct node* node;
    

    to declare a new node. And to avoid that you would have to define later:

    typedef struct node Node;
    

    in order to be able to declare objects like the following:

    Node* node;
    

    In the end:

    typedef struct node {
      int data;
      struct node *next;
    } Node;
    

    Is just a shortcut for struct node { ... }; in addition to typedef struct node Node;.

提交回复
热议问题