【发布时间】:2015-10-22 19:43:21
【问题描述】:
我有 2 个文件 file1.c file2.c。我需要将从 file1.c 传递到 file2.c 的函数指针存储在一个大小为 2 的结构数组中。
file1.c
main ()
{
setFuncPointer ( 1, &call_to_routine_1 );
setFuncPointer ( 2, &call_to_routine_2 );
void call_to_routine_1 ()
{
// Do something
}
void call_to_routine_2()
{
// Do something
}
}
file2.c
struct store_func
{
UINT32 Id;
void *fn_ptr;
} func[2];
void setFuncPointer( UINT32 id, void(*cal_fun)())
{
func[0].id = id;
/* How to assign the cal_fun to the local fn_ptr and use that later in the code */
}
另外,我不确定在结构中声明 void 指针。请提出正确的方法来定义和使用 file1.c from file2.c 中定义的回调函数
提前致谢
【问题讨论】:
-
将函数指针分配给像
void *这样的对象指针是未定义的行为。使用正确的指针类型。 -
非常感谢。但我不确定将函数指针在本地分配给某个变量/指针并稍后在 file2.c 代码中使用相同的变量/指针的正确方法。
标签: c