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

前端 未结 6 1330
醉话见心
醉话见心 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: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 );
    

提交回复
热议问题