C语言函数指针变量运行错误

例子是这个网页上的:http://see.xidian.edu.cn/cpp/html/82.html
#include <stdio.h>

int max(int a,int b){
if(a>b)return a;
else return b;
}
void main(){
int max(int a,int b);
int(*pmax)();
int x,y,z;
pmax=max;
printf("input two numbers:\n");
scanf("%d%d",&x,&y);
z=(*pmax)(x,y);
printf("maxmum=%d",z);

}
调试环境为VS2010,报错为:“int (__cdecl *)(void)”: 用于调用的参数太多
我试过一个变量的可以,两个变量的函数就出错。

函数指针,要指向具有相同参数的函数,修改如下:
#include <stdio.h>
int max(int a,int b)
{
if(a>b)return a;
else return b;
}
void main()
{
int (*pmax)(int,int);//指向具有两个变量的函数指针,这样就可以了
int x,y,z;
pmax = max;
printf("input two numbers:\n");
scanf("%d%d",&x,&y);
z=(*pmax)(x,y);
printf("maxmum=%d\n",z);

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-05-27
int(*pmax)();改成int(*pmax)(int,int);

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网