指针数组函数的用法

来源:百度知道 编辑:UC知道 时间:2024/05/22 14:30:04
const int (*temp[2])(int a,int b);
temp[0]=fun1(3,6);
temp[1]=fun2(7,4);
这段代码编译不通过,为什么呢,请教高手
const int fun1(int a,int b)
{
return a+b;
};
const int fun2(int a,int b)
{
return a-b;
};
const int (*temp[2])(int a,int b);
temp[0]=fun1(3,6);
temp[1]=fun2(7,4);

请参考:
#include <stdio.h>

const int fun1(int a,int b)
{
return a+b;
};
const int fun2(int a,int b)
{
return a-b;
};

typedef const int (* Fun)(int a,int b); //把 int 返回值 两个int参数的函数定义成Fun函数指针类型

int main()
{
Fun f[2]={fun1,fun2}; //形式相同的函数指针数组,方便函数的统一管理
int temp[2];
/*
int i;
for(i=0;i<2;i++)
temp[i]=f[i](1,1);
*/
temp[0]=f[0](3,6);
temp[1]=f[1](7,4);
printf("%d %d\n",temp[0],temp[1]);
return 0;
}

应该这样定义:const int ((*temp)(int a,int b))[]; 但是函数不能返回一个数组. 这样也编不过.