如何定义函数指针数组并且函数没有相同的输入参数定义?

是否可以定义一个函数指针数组(并且函数没有相同的输入参数),如下面的代码所示?

如果是的话我必须把它放在函数定义int (*handler)(/*what Ihave to put here ?*/);

 struct handler_index { const char *name; int (*handler)(/*what Ihave to put here ?*/); }; int handler0 (int a, int b) { printf("%d\n",a+b); } int handler1 (int a, int b, int c) { printf("%d\n",a+b+c); } int handler2 (int a, int b, int c, int d) { printf("%d\n",a+b+c+d); } const struct handler_index handler_index[] = { [0] = {"handler0", handler0}, [1] = {"handler1", handler1}, [2] = {"handler2", handler3}, }; 

什么都不做:

 int (*handler)(); 

它表示该函数具有未指定(但非可变)的数量和参数类型。

任何返回int且具有固定可变数量参数的函数都可以分配给handler

虽然int (*handler)()确实允许函数的变量数量的参数,但我没有看到任何好处。 当你有一段代码需要一些东西时,函数指针是有用的,找到“正确的事情”(例如将“名称”与其他地方的一些输入进行比较),并调用函数指针来做它必须做的事情。 调用代码的函数指针需要知道函数有多少个参数(如何传递正确的数字和参数顺序。

我实际上根本没有看到任何有意义的使用。 是的,您可以将可变数量的参数传递给函数,但代码必须知道函数采用的参数。

除非在结构的定义中以某种方式指定了参数 – 但是您需要为结构定义不同的内容以允许它。

我建议你需要考虑你想要实现的目标,然后提出解决问题的方法,最有可能使用不同的方法。

什么也没放。 只是空括号。

 int (*handler)();