LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 559|回复: 3

帮我看看这个指针函数错在哪儿?

[复制链接]
发表于 2004-5-25 23:09:00 | 显示全部楼层 |阅读模式
[php]
#include <stdio.h>
main()
{ int x, y;
  int max(int,int);
  int min(int,int);
  int sum(int,int);
  int process(x, y, (int *fun)(int,int));
  printf("Input two number:\n");
  scanf("%d %d", x, y);
  printf("max of input is:%d\",process(x, y, max));
}

int max(int a, int b)
{ if (a > b)return a;
  else return b;
}

int min(int a, int b)
{ if (a < b)return a;
  else return b;
}

int sum(int a, int b)
{ return (a + b);}
[/php]
出错:

  1. [linuxer@mydesktop bmp]$ gcc -o ttt.o ttt.c
  2. ttt.c: In function `main':
  3. ttt.c:7: parse error before '(' token
  4. ttt.c:7: `process' declared as function returning a function
  5. ttt.c: In function `process':
  6. ttt.c:7: parse error before ')' token
  7. ttt.c:10:10: warning: multi-line string literals are deprecated
  8. ttt.c:10:10: missing terminating " character
  9. ttt.c:10:10: possible start of unterminated string literal
复制代码
发表于 2004-5-26 00:34:40 | 显示全部楼层

嗯~

1.
[php] int process(x, y, (int *fun)(int,int));
//这个函数这样定义:
int process(int, int, int fun(int,int));

//要记得写函数体:
int process(int x, int y, int fun(int,int))
{
  return fun(x,y);
}
[/php]
2.
[php] scanf("%d %d", x, y);
//这个调用有错,应改为:
scanf("%d %d", &x, &y);
[/php]
3.
[php]
printf("max of input is : %d\",process(x, y, max));
//这里是粗心吧??改成:
printf("max of input is : %d\n",process(x, y, max));
//仔细看看有什么不同。
[/php]
发表于 2004-5-26 10:23:35 | 显示全部楼层
[PHP]
#include <iostream>
using namespace std;
  int x, y;
int max(int,int);
int min(int,int);
int sum(int,int);
int process(int,int,int (*fun)(int,int));
int main() {
       
         cout<<"Input two number:"<<"\n";
         cin>>x;
         //cin.get();
         cin>>y;
         cout<<"max of input is:"<<process(x, y, min);
        }

int max(int a, int b) {
        if (a > b)return a;
        else return b;
}

int min(int a, int b)
{ if (a < b)return a;
  else return b;
}

int sum(int a, int b) {
         return (a + b);
        }
int process(int x,int  y, int (*fun)(int,int)){
        int result;
        result=(*fun)(x,y);
        return result;
}
       

[/PHP]
 楼主| 发表于 2004-5-26 21:22:53 | 显示全部楼层
:thank
短短几行代码我就错了这么多,真是遗笑大方了:p
看来路还好长。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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