您的位置:首页 > 其它

函数指针数组

2015-12-23 19:49 141 查看
// 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 指针 typedef