【发布时间】:2021-02-13 18:12:39
【问题描述】:
我对如何实现以下内容有点困惑。我想要一个函数,func2,返回一个函数,调用 func1 并指定参数:
int func1(int x, int y, int z, int type){
// Calculations
}
int ((*func2)(int x, int y, int z))(int type){
// Return a pointer to func1 that with get x, y, z as parameters
// when called later, with type = type being fixed
}
用途:
my_func = func2(3);
printf("result = %d\n", my_func(1,2,3));
【问题讨论】:
-
这能回答你的问题吗? Is there a a way to achieve closures in C
-
这个问题有很多现有的答案。请参阅重复链接。有些使用FFCALL,有些只是直接C。
-
嗯,看起来 GNU 也有一个 callback library。这些解决方案的优点是它们为您提供了一个具有自己关联状态的可调用函数。不幸的是,没有办法直接在 C 中实现。
标签: c function pointers parameters