鱼C论坛

 找回密码
 立即注册
查看: 3355|回复: 7

帮忙看看这段程序什么问题,指向函数的指针

[复制链接]
发表于 2012-3-22 22:17:31 | 显示全部楼层 |阅读模式
3鱼币
  1. #include<stdio.h>
  2. void main()
  3. {
  4.       int max(int,int);
  5.       int (*p)();
  6.       int a,b,c;
  7.       p=max;
  8.       scanf("%d %d ",&a,&b);
  9.       c=(*p)(a,b);
  10.       printf("max=%d",c);

  11. }   
  12. int max(int x,int y)
  13. {
  14.       int z;
  15.       z=x>y?x:y;
  16.       return z;
  17. }错误提示f:\c\esr\r.cpp(7) : error C2440: '=' : cannot convert from 'int (__cdecl *)(int,int)' to 'int (__cdecl *)(void)'This conversion requires a reinterpret_cast, a C-style cast or function-style cast
  18. f:\c\esr\r.cpp(9) : error C2197: 'int (__cdecl *)(void)' : too many actual parameters
复制代码

最佳答案

查看完整内容

int max(int,int); int (*p)(); 改成 int (*p)(int,int);
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-3-22 22:17:32 | 显示全部楼层
int max(int,int);
int (*p)(); 改成 int (*p)(int,int);
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-22 23:27:58 | 显示全部楼层
#include <stdio.h>

int max(int x,int y)
{
      int z;
      z=x>y?x:y;
      return z;
}
int main()
{
      int max(int,int);
      int (*p)();
      int a,b,c;
      p=&max;
      scanf("%d%d",&a,&b);
      c=(*p)(a,b);
      printf("max=%d",c);
      return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-22 23:28:22 | 显示全部楼层
指针取地址&
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-3-23 07:15:34 | 显示全部楼层
谢谢 两位,2楼是正解。程序照书上打的,看来是书上的打印错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-23 10:46:38 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-3-24 10:29:34 | 显示全部楼层
wAterLoo 发表于 2012-3-23 10:46
你从哪本书上照着打的?我也去看看

C语言从初学到精通  电子工业出版社
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-24 10:36:52 | 显示全部楼层
新情不好 发表于 2012-3-24 10:29
C语言从初学到精通  电子工业出版社

在多少章节
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-12 07:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表