我怎样才能使用一个函数指针数组?

我应该如何使用C中的函数指针数组?

我怎样才能初始化它们?

你有一个很好的例子(函数指针数组) , 详细的语法 。

int sum(int a, int b); int subtract(int a, int b); int mul(int a, int b); int div(int a, int b); int (*p[4]) (int x, int y); int main(void) { int result; int i, j, op; p[0] = sum; /* address of sum() */ p[1] = subtract; /* address of subtract() */ p[2] = mul; /* address of mul() */ p[3] = div; /* address of div() */ [...] 

要调用这些函数指针之一:

 result = (*p[op]) (i, j); // op being the index of one of the four functions 

上面的答案可能会帮助你,但你也可能想知道如何使用函数指针数组。

 void fun1() { } void fun2() { } void fun3() { } void (*func_ptr[3]) = {fun1, fun2, fun3}; main() { int option; printf("\nEnter function number you want"); printf("\nYou should not enter other than 0 , 1, 2"); /* because we have only 3 functions */ scanf("%d",&option); if((option>=0)&&(option<=2)) { (*func_ptr[option])(); } return 0; } 

您只能将具有相同返回types和相同参数types以及不带参数的函数地址分配给单个函数指针数组。

如果所有上述函数具有相同types的相同数量的参数,那么也可以像下面那样传递参数。

  (*func_ptr[option])(argu1); 

注意:这里在数组中,函数指针的编号将从0开始,与一般数组相同。 所以在上面的例子中,如果选项= 0,则可以调用fun2如果选项= 1, fun3可以调用fun2如果选项= 2, fun3可以调用fun3

以下是您可以如何使用它的方法:

New_Fun.h

 #ifndef NEW_FUN_H_ #define NEW_FUN_H_ #include <stdio.h> typedef int speed; speed fun(int x); enum fp { f1, f2, f3, f4, f5 }; void F1(); void F2(); void F3(); void F4(); void F5(); #endif 

New_Fun.c

 #include "New_Fun.h" speed fun(int x) { int Vel; Vel = x; return Vel; } void F1() { printf("From F1\n"); } void F2() { printf("From F2\n"); } void F3() { printf("From F3\n"); } void F4() { printf("From F4\n"); } void F5() { printf("From F5\n"); } 

MAIN.C

 #include <stdio.h> #include "New_Fun.h" int main() { int (*F_P)(int y); void (*F_A[5])() = { F1, F2, F3, F4, F5 }; // if it is int the pointer incompatible is bound to happen int xyz, i; printf("Hello Function Pointer!\n"); F_P = fun; xyz = F_P(5); printf("The Value is %d\n", xyz); //(*F_A[5]) = { F1, F2, F3, F4, F5 }; for (i = 0; i < 5; i++) { F_A[i](); } printf("\n\n"); F_A[f1](); F_A[f2](); F_A[f3](); F_A[f4](); return 0; } 

我希望这有助于理解Function Pointer.

哦,有很多例子。 只要看看glib或gtk中的任何东西。 你可以一路看到函数指针的工作。

在这里例如gtk_button的东西的初始化。

 static void gtk_button_class_init (GtkButtonClass *klass) { GObjectClass *gobject_class; GtkObjectClass *object_class; GtkWidgetClass *widget_class; GtkContainerClass *container_class; gobject_class = G_OBJECT_CLASS (klass); object_class = (GtkObjectClass*) klass; widget_class = (GtkWidgetClass*) klass; container_class = (GtkContainerClass*) klass; gobject_class->constructor = gtk_button_constructor; gobject_class->set_property = gtk_button_set_property; gobject_class->get_property = gtk_button_get_property; 

在gtkobject.h中,您可以find以下声明:

 struct _GtkObjectClass { GInitiallyUnownedClass parent_class; /* Non overridable class methods to set and get per class arguments */ void (*set_arg) (GtkObject *object, GtkArg *arg, guint arg_id); void (*get_arg) (GtkObject *object, GtkArg *arg, guint arg_id); /* Default signal handler for the ::destroy signal, which is * invoked to request that references to the widget be dropped. * If an object class overrides destroy() in order to perform class * specific destruction then it must still invoke its superclass' * implementation of the method after it is finished with its * own cleanup. (See gtk_widget_real_destroy() for an example of * how to do this). */ void (*destroy) (GtkObject *object); }; 

(* set_arg)是一个指向函数的指针,例如可以在派生类中指派另一个实现。

你经常看到这样的事情

 struct function_table { char *name; void (*some_fun)(int arg1, double arg2); }; void function1(int arg1, double arg2).... struct function_table my_table [] = { {"function1", function1}, ... 

所以你可以通过名字进入表格并调用“关联”function。

或者,也许你使用了一个哈希表,在其中放置该函数,并称之为“按名称”。

问候
弗里德里希

这个“答案”更多的是VonC答案的附录。 只是注意到可以通过typedef简化语法,可以使用聚合初始化:

 typedef int FUNC(int, int); FUNC sum, subtract, mul, div; FUNC *p[4] = { sum, subtract, mul, div }; int main(void) { int result; int i = 2, j = 3, op = 2; // 2: mul result = p[op](i, j); // = 6 } // maybe even in another file int sum(int a, int b) { return a+b; } int subtract(int a, int b) { return ab; } int mul(int a, int b) { return a*b; } int div(int a, int b) { return a/b; } 

这应该是一个简单的复制和粘贴上面的回应代码示例。 希望这有助于。

 #include <iostream> using namespace std; #define DBG_PRINT(x) do { std::printf("Line:%-4d" " %15s = %-10d\n", __LINE__, #x, x); } while(0); void F0(){ printf("Print F%d\n", 0); } void F1(){ printf("Print F%d\n", 1); } void F2(){ printf("Print F%d\n", 2); } void F3(){ printf("Print F%d\n", 3); } void F4(){ printf("Print F%d\n", 4); } void (*fArrVoid[N_FUNC])() = {F0, F1, F2, F3, F4}; int Sum(int a, int b){ return(a+b); } int Sub(int a, int b){ return(ab); } int Mul(int a, int b){ return(a*b); } int Div(int a, int b){ return(a/b); } int (*fArrArgs[4])(int a, int b) = {Sum, Sub, Mul, Div}; int main(){ for(int i = 0; i < 5; i++) (*fArrVoid[i])(); printf("\n"); DBG_PRINT((*fArrArgs[0])(3,2)) DBG_PRINT((*fArrArgs[1])(3,2)) DBG_PRINT((*fArrArgs[2])(3,2)) DBG_PRINT((*fArrArgs[3])(3,2)) return(0); } 

这个问题已经被很好的例子解答了。 唯一可能会丢失的例子是函数返回指针的地方。 我写了另一个例子,并添加了很多评论,以防有人发现它有帮助:

 #include <stdio.h> char * func1(char *a) { *a = 'b'; return a; } char * func2(char *a) { *a = 'c'; return a; } int main() { char a = 'a'; /* declare array of function pointers * the function pointer types are char * name(char *) * A pointer to this type of function would be just * put * before name, and parenthesis around *name: * char * (*name)(char *) * An array of these pointers is the same with [x] */ char * (*functions[2])(char *) = {func1, func2}; printf("%c, ", a); /* the functions return a pointer, so I need to deference pointer * Thats why the * in front of the parenthesis (in case it confused you) */ printf("%c, ", *(*functions[0])(&a)); printf("%c\n", *(*functions[1])(&a)); a = 'a'; /* creating 'name' for a function pointer type * funcp is equivalent to type char *(*funcname)(char *) */ typedef char *(*funcp)(char *); /* Now the declaration of the array of function pointers * becomes easier */ funcp functions2[2] = {func1, func2}; printf("%c, ", a); printf("%c, ", *(*functions2[0])(&a)); printf("%c\n", *(*functions2[1])(&a)); return 0; } 

这个简单的示例multidimensional array与函数指针“:

 void one( int a, int b){ printf(" \n[ ONE ] a = %db = %d",a,b);} void two( int a, int b){ printf(" \n[ TWO ] a = %db = %d",a,b);} void three( int a, int b){ printf("\n [ THREE ] a = %db = %d",a,b);} void four( int a, int b){ printf(" \n[ FOUR ] a = %db = %d",a,b);} void five( int a, int b){ printf(" \n [ FIVE ] a = %db = %d",a,b);} void(*p[2][2])(int,int) ; int main() { int i,j; printf("multidimensional array with function pointers\n"); p[0][0] = one; p[0][1] = two; p[1][0] = three; p[1][1] = four; for ( i = 1 ; i >=0; i--) for ( j = 0 ; j <2; j++) (*p[i][j])( (i, i*j); return 0; } 

最简单的解决方法是给出你想要的最终vector的地址,并在函数内修改它。

 void calculation(double result[] ){ //do the calculation on result result[0] = 10+5; result[1] = 10 +6; ..... } int main(){ double result[10] = {0}; //this is the vector of the results calculation(result); //this will modify result }