函数指针
1
| void func(int a,int b , int (*func2)(int x, int y));
|
函数指针function pointer通常应用于菜单驱动的系统中。系统提示用户从菜单中选择一种操作,每个选项的操作都是由不同过的函数来完成的。指向每个函数的指针就存储在一个指针数组中。用户的选择将作为数组的下标,并且数组中的指针被用于调用相应的函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <stdio.h>
void function1(int a, int b); void function2(int a, int b); void function3(int a, int b);
int main(void) { void (*f[3])(int ,int) = {function1,function2,function3}; int choice;
printf("Enter a number between 0 and 2, 3 to end:"); scanf("%d",&choice);
while(choice >= 0 && choice < 3) { (*f[choice])(choice); printf("Enter a number between 0 and 2, 3 to end:"); scanf("%d",&choice); }
printf("Program execution completed.\n"); return 0; }
void function1(int a, int b) { printf("You are now using function1"); }
void function2(int a, int b) { printf("You are now using function1"); }
void function3(int a, int b) { printf("You are now using function1"); }
|