![](/img/trans.png)
[英]How can i initialize and print out a Structure with a pointer and give it to a function?
[英]How can I store a function pointer in a structure?
我声明了typedef void (*DoRunTimeChecks)();
如何将其存储为结构中的字段? 我该如何分配? 我如何调用fn()?
就像你在任何其他领域一样把它放进去:
struct example {
int x;
DoRunTimeChecks y;
};
void Function(void)
{
}
struct example anExample = { 12, Function };
要分配到该字段:
anExample.y = Function;
要调用该函数:
anExample.y();
#include <stdio.h>
typedef void (*DoRunTimeChecks)();
struct func_struct {
DoRunTimeChecks func;
};
void function()
{
puts("hello");
}
int main()
{
struct func_struct func_struct;
func_struct.func = function;
func_struct.func();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.