C: Function pointer inside a typedef struct

后端 未结 3 792
被撕碎了的回忆
被撕碎了的回忆 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:14

    You need to assign the function to the member. i also recommend giving them different names:

    typedef void (*addMSGFunc)(unsigned char *, int, struct linkedList *);
    
    typedef struct linkedList {
        int count;
        struct msgNode *front;
        struct msgNode *back;
        addMSGFunc addMSG;
    } msgList;
    
    void addMSGImpl(unsigned char *data, int size, struct linkedList *self)
    {
        ...
    }
    

    And then after creating a msgList:

    msgList myList;
    myList.addMSG = addMSGImpl;
    

提交回复
热议问题