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

前端 未结 6 1304
醉话见心
醉话见心 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:46

    Does it have something to do with the fact that if you don't include a lower case node then when the compiler is evaluating the code it will not be able to understand what is meant by "struct node *next"?

    Yes.

    The node in struct node is the tag of the struct type. If you give the struct a tag, you can refer to that type from the moment on the tag is complete, so in

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

    the struct node *next; declares a member next that is a pointer to the struct type being defined. The typedef name Node is not available before the ; ending the definition is reached.

    If you omit the tag, you cannot refer to the type being defined in any way before the typedef is complete, so in

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

    the line struct node *next; declares a new, unrelated, incomplete struct type with the tag node that next points to.

    That's valid, but nothing about struct node is known (unless it is defined somewhere else), so you can't use the next pointer without casting it to a pointer to a complete type everywhere (not quite everywhere, Node foo; foo.next = malloc(12); etc. would still work).

    0 讨论(0)
  • 2020-12-30 08:46

    Consider this code:

    #include <stdio.h>
    
    typedef struct {
       int data;
       struct node *next;
    } Node;
    
    int main()
    {
       Node a, b = {10, NULL};
       a.next = &b;
    
       printf("%d\n", a.next->data);
    }
    

    This won't compile. The compiler has no idea what a struct node is, other than it exists. So you might change the definition in the struct to Node *next;. The typedef isn't in scope before it's declared, so it still won't compile. The simple answer is to do as he said, use the node tag after struct, and it works fine.

    0 讨论(0)
  • 2020-12-30 08:50

    Here struct node is a type like int

    and Hence

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

    means you are declaring a single variable Node of struct node.

    like int intVar;

    typedef is to make your code understandable.

    so that when you use

    typedef struct node Node;
    

    you can use the same declaration as

    Node NodeVar;
    
    0 讨论(0)
  • 2020-12-30 08:55

    The lower case 'node' is a structure type... i.e. a struct node { stuff } is a node structure containing stuff.

    On the other hand, the upper case "Node" is a completely new data type which refers to a 'struct node'

    Generally (though in C++ I think you can), you cannot pass around a "node" in a C program... for example as an argument to a function. Rather, you would have to pass a 'struct node' as your argument...

    // this will throw a syntax error because "node" is not a data type, 
    // it's a structure type.
    
    void myFunc( node* arg ); 
    
    // while this will not because we're telling the compiler we're 
    // passing a struct of node
    
    void myFunc( struct node* arg ); 
    
    // On the other hand, you *can* use the typedef shorthand to declare 
    // passing a pointer to a custom data type that has been defined 
    // as 'struct node'
    
    void myFunc( Node* arg );
    
    0 讨论(0)
  • 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;.

    0 讨论(0)
  • 2020-12-30 09:00

    Take a look at this declaration:

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

    This can be combined into a single statement (simplifying a declaration):

    typedef struct node {
      int data;
      struct node *next;
    } Node;
    
    0 讨论(0)
提交回复
热议问题