对于有限状态机的实现,我想实现以下功能:
currentFunction = currentFunction(int arg);
与currentFunction
作为一个函数指针返回相同功能类型的函数指针的函数。
这在C中可能吗?如何?
首先,要做这样真正晦涩的事情总是源于程序设计的泥泞。您正在尝试解决问题X,并且您认为方法Y可以解决。所以您问我们如何使Y起作用,即使最开始时它可能不是正确的解决方案。问题的根源可能是X的设计不正确。
话虽如此,您可以通过将结构用作包装器并利用不完整的结构类型来做到这一点:
typedef struct func_wrapper_t
{
struct func_wrapper_t (*func) (int);
} func_t;
...
func_t some_function (int x)
{
...
return (func_t){ some_function };
}
...
int main(void)
{
func_t f = some_function(0);
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句