How do I refer to a typedef in a header file?

后端 未结 2 1678
北荒
北荒 2021-01-03 00:02

I have a source file where a typedef struct is defined:

typedef struct node {
    char *key;
    char *value;
    struct node *next;
} *Node;
2条回答
  •  春和景丽
    2021-01-03 00:06

    You can use:

    typedef struct node * Node;
    

    But I would advise against hiding the pointer in type declaration. It is more informative to have that information in variable declaration.

    module.c:

    #include "module.h"
    struct node {
        char *key;
        char *value;
        struct node *next;
    };
    

    module.h:

    typedef struct node Node;
    

    variable declaration for pointer somewhere:

    #include "module.h"
    Node * myNode; // We don't need to know the whole type when declaring pointer
    

提交回复
热议问题