C语言 C:typedef 结构内的函数指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19039014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: Function pointer inside a typedef struct
提问by Edwin
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.
我试图在 C 中创建一个链表,但试图在某种 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. 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?
目前,当我调用 addMSG 时出现分段错误,这当然是因为 addMSG 没有指向函数。但是,我不想在每次要使用链表时都指定一个函数来指向它。有没有什么好的方法可以让函数指针不必隐式指向函数,或者你是否必须隐式指向函数?
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.
这只是此处显示的部分实现。最后,这个结构将具有所有必要的功能。这只是为了使这个问题简短而切中要害。
回答by Asaf
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;
回答by Rudolfs Bundulis
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
linkedListinstance - I guess you've seen that in C style libraries - Create a default list item and use that when creating new entities.
- 创建一个函数来初始化
linkedList实例 - 我猜你已经在 C 样式库中看到了 - 创建一个默认列表项并在创建新实体时使用它。
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};
回答by Crowman
You can have an uninitialized function pointer just fine as long as you don't actually use it. If you dowant 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 NULLto 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.
如果它总是指向同一个函数,那么只需在构造函数中进行赋值即可。

