// tas.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
typedef int (*Funs)(int, int);//利用typedef定义一个函数指针别名
int add(int a,int b){
return a+b;
}
int mul(int a,int b){
return a*b;
}
int main(int argc, char* argv[])
{
int x,y;
void *a[]={add,mul};//定义了指针数组,这里a是一个普通指针
a[0](8,10);//编译错误,指针数组不能用下标的方式来调用函数
Funs tasksArr[]={//定义一个函数指针数组,数组里面有两个函数
add,//第一个函数,注意只有函数名,写成add(8,10)等是错误的
mul//第二个函数
};
x=tasksArr[0](8,10);//调用函数指针数组中的第一个函数
y=tasksArr[1](8,10);//调用函数指针数组中的第二个函数
printf("%d\n%d\n",x,y);
return 0;
}
可参考http://jingyan.baidu.com/article/d8072ac47ad60fec95cefd31.html