[英]How to pass an array of function pointers as an argument?
I have an array of function pointers int (*oper_ptr[4])(int, int) = {add, sub, mul, divi};
我有一个函数指针数组int (*oper_ptr[4])(int, int) = {add, sub, mul, divi};
for the below functions which simply perform a standard operation on two passed integers:对于以下仅对两个传递的整数执行标准操作的函数:
int add(int num_a, int num_b);
int sub(int num_a, int num_b);
int mul(int num_a, int num_b);
int divi(int num_a, int num_b);
What is the best way to pass this array into another function.将此数组传递给另一个函数的最佳方法是什么。 I have tried: void polish(char* filename, int (*oper_ptr[4])(int, int))
with for eg, oper_ptr[0](num_a, num_b);
我试过: void polish(char* filename, int (*oper_ptr[4])(int, int))
例如, oper_ptr[0](num_a, num_b);
to call the first function.调用第一个函数。
The way you have done it works.你完成它的方式是有效的。 But as always with function pointers, it's a bad idea to use them without typedef:但与函数指针一样,在没有 typedef 的情况下使用它们是个坏主意:
typedef int operation (int num_a, int num_b);
void polish (char* filename, operation* op[4]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.