我如何键入一个函数指针,该函数指针将自己类型的函数作为参数?

StackOverflow https://stackoverflow.com/questions/816356

  •  03-07-2019
  •  | 
  •  

示例:一个函数,它接受一个函数(带有一个函数(那个......)和一个int)和一个int。

typedef void(*Func)(void (*)(void (*)(...), int), int);

它在(...)的地方递归爆炸。是否存在无法完成的根本原因或是否存在其他语法?在我看来,没有演员阵容应该是可能的。我真的想传递一个调度表,但如果我能通过这一种类型,我可以想出来。

有帮助吗?

解决方案

您可以将函数指针包装在struct:

struct fnptr_struct;
typedef void (*fnptr)(struct fnptr_struct *);
struct fnptr_struct {
  fnptr fp;
};

我不确定这是否是对铸造的改进。我怀疑没有结构是不可能的,因为C要求在使用之前定义类型,并且typedef没有不透明的语法。

其他提示

直接做是不可能的。您唯一的选择是使函数指针参数接受未指定的参数,或接受指向包含函数指针的结构的指针,如 Dave建议

// Define fnptr as a pointer to a function returning void, and which takes one
// argument of type 'pointer to a function returning void and taking
// an unspecified number of parameters of unspecified types'
typedef void (*fnptr)(void (*)());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top