undefined C struct forward declaration

后端 未结 4 1598
感情败类
感情败类 2020-12-05 14:39

I have a header file port.h, port.c, and my main.c

I get the following error: \'ports\' uses undefined struct \'port_t\'

I thought as I have declared the str

4条回答
  •  被撕碎了的回忆
    2020-12-05 15:13

    A common solution that I use:

    /* port.h */
    typedef struct port_t *port_p;
    
    /* port.c */
    #include "port.h"
    struct port_t
    {
        unsigned int port_id;
        char name;
    };
    

    You use the port_p in function interfaces. You will need to create special malloc (and free) wrappers in port.h as well:

    port_p portAlloc(/*perhaps some initialisation args */);
    portFree(port_p);
    

提交回复
热议问题