C: Function pointer inside a typedef struct

后端 未结 3 752
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 16:29

I am trying to create a linked list in C but trying to pack it nicely in somewhat of a C++ style class. I am having some issues however using function pointers in C.

<         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 17:21

    Well you can't add a default value in the declaration of the struct but what you can do is:

    • Create a function to initialize the linkedList instance - I guess you've seen that in C style libraries
    • Create a default list item and use that when creating new entities.

    Like:

    void addMSG(unsigned char *data, int size, struct linkedList *self);
    
    struct linkedList {
        int count;
        struct msgNode *front;
        struct msgNode *back;
        void (*addMSG)(unsigned char *, int, struct linkedList *);
    } DefaultList = {0, NULL, NULL, addMSG};
    

提交回复
热议问题