饭浇盖 发表于 2016-3-5 20:40:59

为什么

#include <stdio.h>

void main()
{
      int max(int, int);            /* 函数声明 */
      int min(int, int);            /* 函数声明 */
      int add(int, int);            /* 函数声明 */
   
      void process( int, int, int(*fun)() );    /* 函数声明 */
      
      int a, b;

      printf("Endter a and b: ");
      scanf("%d %d", &a, &b);
      
      printf("max = ");
      process(a, b, max);

      printf("min = ");
      process(a, b, min);

      printf("sum = ");
      process(a, b, add);
}

int max(int x, int y)            /* 函数定义 */
{
      int z;
      
      if( x > y )
      {
            z = x;
      }
      else
      {
            z = y;
      }

      return z;
}

int min(int x, int y)            /* 函数定义 */
{
      int z;

      if( x < y )
      {
            z = x;
      }
      else
      {
            z = y;
      }

      return z;
}

int add(int x, int y)
{
      int z;
      
      z = x + y;
      return z;
}

void process( int x, int y, int(*fun)() )    /* 函数定义 */
{
      int result;

      result = (*fun)(x, y);
      printf("%d\n", result);
}



这个是C语言教程中的代码 为什么我复制一下到新建的编辑器中就会出现下面这样的错误



对不起各位,麻烦各位了 我是菜鸟

阴影中的曙光 发表于 2016-3-5 20:52:38

类型不匹配#include <stdio.h>

int main()
{
    int max(int, int);            /* 函数声明 */
    int min(int, int);            /* 函数声明 */
    int add(int, int);            /* 函数声明 */
   
    void process( int, int, int(fun)(int,int) );    /* 函数声明 */
                                     //不是int(*fun)(),因为你max等函数都是这么声明的
    int a, b;
   
    printf("Endter a and b: ");
    scanf("%d %d", &a, &b);
   
    printf("max = ");
    process(a, b, max);
   
    printf("min = ");
    process(a, b, min);
   
    printf("sum = ");
    process(a, b, add);
}

int max(int x, int y)            /* 函数定义 */
{
    int z;
   
    if( x > y )
    {
      z = x;
    }
    else
    {
      z = y;
    }
   
    return z;
}

int min(int x, int y)            /* 函数定义 */
{
    int z;
   
    if( x < y )
    {
      z = x;
    }
    else
    {
      z = y;
    }
   
    return z;
}

int add(int x, int y)
{
    int z;
   
    z = x + y;
    return z;
}

void process( int x, int y, int(fun)(int,int) )    /* 函数定义 */
{
    int result;
   
    result = (fun)(x,y);
    printf("%d\n", result);
}

饭浇盖 发表于 2016-3-5 21:22:17

阴影中的曙光 发表于 2016-3-5 20:52
类型不匹配

谢谢,意思就是要前后对应,是吧

13751652677 发表于 2016-3-6 00:15:44

也可以这样
#include <stdio.h>

int main()
{
    int max(int, int);            /* 函数声明 */
    int min(int, int);            /* 函数声明 */
    int add(int, int);            /* 函数声明 */

    void process( int, int, int(*fun)(int,int) );    /* 函数声明 */
                                     //不是int(*fun)(),因为你max等函数都是这么声明的
    int a, b;

    printf("Endter a and b: ");
    scanf("%d %d", &a, &b);

    printf("max = ");
    process(a, b, &max);

    printf("min = ");
    process(a, b, &min);

    printf("sum = ");
    process(a, b,&add);
}

int max(int x, int y)            /* 函数定义 */
{
    int z;

    if( x > y )
    {
      z = x;
    }
    else
    {
      z = y;
    }

    return z;
}

int min(int x, int y)            /* 函数定义 */
{
    int z;

    if( x < y )
    {
      z = x;
    }
    else
    {
      z = y;
    }

    return z;
}

int add(int x, int y)
{
    int z;

    z = x + y;
    return z;
}

void process( int x, int y, int(fun)(int,int) )    /* 函数定义 */
{
    int result;

    result = (fun)(x,y);
    printf("%d\n", result);
}
页: [1]
查看完整版本: 为什么