C circular dependency

前端 未结 3 1405
予麋鹿
予麋鹿 2020-11-30 13:24

I have this problem with circular dependency in C, I looked around the other questions about this topic but really couldn\'t find the answer.

I hav

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 13:33

    This kind of dependency is broken using a forward declaration. Instead of including a file with the full definition of the struct, there are two alternatives:

    1.

    typedef struct 
    {
        char* name;
        float x, y;
        struct _edgelist* edges; /* add "struct" here (elaborated type specifier) */
    } vertex;
    

    2.

    struct __edgelist; /* better form: forward declaration */
    
    typedef struct 
    {
        char* name;
        float x, y;
        struct _edgelist* edges; /* still need to add "struct" here */
    } vertex;
    

提交回复
热议问题