简体   繁体   English

C:typedef结构中的函数指针

[英]C: Function pointer inside a typedef struct

I am trying to create a linked list in C but trying to pack it nicely in somewhat of a C++ style class. 我试图在C中创建一个链接列表,但试图在一些C ++样式类中很好地打包它。 I am having some issues however using function pointers in C. 我有一些问题,但在C中使用函数指针。

typedef struct linkedList {
    int count;
    struct msgNode *front;
    struct msgNode *back;
    void (*addMSG)(unsigned char *, int, struct linkedList *);
} msgList;

void addMSG(unsigned char *data, int size, struct linkedList *self);

Ideally, I would like to have it such that you can make you list and then to add you can simply call a "method"(function) within the structure, simulating behavior you would see in C++. 理想情况下,我希望你可以让你列出然后添加你可以简单地在结构中调用一个“方法”(函数),模拟你在C ++中看到的行为。

Currently I get a segmentation fault when I call addMSG, which of-course is because addMSG is not pointing to a function. 目前,当我调用addMSG时,我遇到了分段错误,当然这是因为addMSG没有指向某个函数。 However, I don't want to have to specify a function to point to every single time I want use a linked list. 但是,我不想指定一个函数来指向我想要使用链表的每一次。 Is there any nice way to have function pointers without implicitly having to point to the function, or do you have to implicitly point it to the function? 有没有很好的方法来获得函数指针而不必隐式地指向函数,或者你是否必须隐式地指向函数?

This is only the partial implementation shown here. 这只是这里显示的部分实现。 At the end, this struct will have all the necessary functions. 最后,这个结构将具有所有必要的功能。 This is just for the sake of keeping this question short and to the point. 这只是为了保持这个问题简短而重要。

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之后:

msgList myList;
myList.addMSG = addMSGImpl;

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 创建一个函数来初始化linkedList实例 - 我想你已经在C风格库中看到了它
  • 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};

You can have an uninitialized function pointer just fine as long as you don't actually use it. 只要不实际使用它,就可以使用未初始化的函数指针。 If you do want to use it to call a function, then obviously you have to assign a function to it. 如果想用它来调用一个函数,那么很明显你有一个函数分配给它。 C is unable to guess which function you want to use. C无法猜出您要使用哪个功能。

If you have a linked list structure where sometimes you need a function, and sometimes you don't, then just assign NULL to it when you create the list, and have your list implementation only call the function when it's not NULL . 如果你有一个链表结构,有时你需要一个函数,有时你不需要,那么只需在创建列表时为它指定NULL ,并让你的列表实现只在函数不为NULL时调用它。

If it always points to the same function, then just do the assignment in your constructor function. 如果它总是指向同一个函数,那么只需在构造函数中进行赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM