C:如何将单个函数指针数组用于具有可变参数计数的函数?
这个问题几乎说明了一切。
我不知道如何做到这一点,并且还没有接近任何有效的方法。
这里有一些示例函数:
add(int x, int y) {
return x+y;
}
并且,
mean(int x1, int y1, int x2, int y2) {
return (x1 + y1 + x2 + y2) / 4;
}
到目前为止,我已经尝试将 typedef 与两者一起使用,但我不知道如何使某些内容指向任一类型:
typedef int (*mathfunc2)(int x, int y);
typedef int (*mathfunc4)(int x1, int y1, int x2, int y2);
????? func_table[2] = {add, mean};
The question pretty much says it all.
I'm not sure how to do this and haven't come anywhere near anything that works.
Here's some example functions:
add(int x, int y) {
return x+y;
}
and,
mean(int x1, int y1, int x2, int y2) {
return (x1 + y1 + x2 + y2) / 4;
}
So far I've tried using typedef with both, but I can't figure how to make something point to one of either type:
typedef int (*mathfunc2)(int x, int y);
typedef int (*mathfunc4)(int x1, int y1, int x2, int y2);
????? func_table[2] = {add, mean};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要选择一个函数指针类型用作“通用函数指针”,使用该类型来定义数组,并使用显式强制转换。将一种函数指针类型转换为另一种函数指针类型,然后再转换回来,可以保证保留该值。
换句话说:
然后调用
add
,你需要将其转换回正确的类型:如果你觉得它更容易接受,你可以定义一些宏:
You need to pick a function pointer type to use as a "generic function pointer", use that type to define your array, and use explicit casts. Casting one function pointer type to another and then back again is guaranteed to preserve the value.
In other words:
Then to call
add
, you need to cast it back to the right type:You can define some macros if you find it more palatable:
您可以像这样使用 外观模式:
虽然代码很丑陋,但它可以完成工作。您应该在包装器中添加一些验证逻辑。
You could use the Facade Pattern like this:
Although the code is ugly, it does the job. You should add some validation logic in wrappers.
这两种函数类型不兼容。严格来说,它们可以使用完全不同的参数传递来实现。例如,实现可能会选择所有最多具有 3 个参数的函数通过寄存器接收它们,而所有其他函数通过堆栈接收它们。
不过,您可以做的是使用 varargs 参数定义两个函数,以使它们遵循相同的参数传递方案。
无论如何,您打算如何调用这些函数,而不知道它们需要多少个参数?
These two function types are incompatible. Strictly speaking, they could be implemented using completely different argument passing. An implementation might choose, for example, that all functions with up to 3 parameters receive them via registers, and all other functions receive them via the stack.
What you can do though is to define both functions with varargs parameters to make them follow the same parameter passing scheme.
How did you intend to call these functions anyway, not knowing how many parameters they expect?