| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1854 人关注过本帖, 1 人收藏
标题:函数指针void process(int x,int y,int(*fun)(int,int ) )和void process(i ...
只看楼主 加入收藏
原以为
Rank: 2
等 级:论坛游民
帖 子:33
专家分:12
注 册:2015-10-15
结帖率:60%
收藏(1)
已结贴  问题点数:10 回复次数:10 
函数指针void process(int x,int y,int(*fun)(int,int ) )和void process(int x,int y,int(
程序代码:
/*函数指针(此程序可正常运行)*/
#include<stdio.h>                                
int max(int x,int y);                                //函数声明
int min(int x,int y);                                //函数声明
int add(int w,int z);                                //函数声明
void process(int x,int y,int(*fun)(int,int ) );       //函数声明 
/*(当把这句语句改成void process(int x,int y,int(*fun)( ) );后面函数定义时作相应修改)编译时程序错误,为什么?*/

void main()
{
    int a,b;
    printf("请输入两个数字:");
    scanf("%d %d",&a,&b);
    
    
    process(a,b, max);
    process(a,b, min);
    process(a,b, add);
}

int max(int x,int y)
{    
    int  w;
    
    w=x>y?x:y;
    return w;
} 
int min(int x,int y)
{    
    int  w;
    
    w=x<y?x:y;
    return w;
} 
int add(int w,int z)
{
          int x;
          x=w+z;
          return x;
}
void process(int x,int y,int (*fun)(int,int))
{
          int result;
          result=(*fun)(x,y);
          printf("%d\n", result);
          
          
}
搜索更多相关主题的帖子: process 
2015-11-19 13:51
原以为
Rank: 2
等 级:论坛游民
帖 子:33
专家分:12
注 册:2015-10-15
收藏
得分:0 
回复 楼主 原以为
程序代码:
/*        为何这个程序可以正常运行                         */


/***********************************************************/
/*  设一个函数process,在调用它的时候,每次实现不同的功能。*/
/*  输入a和b两个数,第一次调用process时找出a和b中大者,*/
/*  第二次找出其中小者,第三次求a与b之和。               */
#include <stdio.h>
      int max(int x, int y);            /* 函数声明 */
      int min(int x, int y);            /* 函数声明 */
      int add(int x, int y);            /* 函数声明 */  
      void process( int x, int y, int(*fun)() );    /* 函数声明 */
      

void main()
{
     
      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);

 }


[此贴子已经被作者于2015-11-19 13:57编辑过]

2015-11-19 13:54
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:4 
一个有两参数,另一个没有,然后没有那个程序,你result = (*fun)(x, y);去调用有参数的,编译器会告诉你找不到对应的函数原形。

int(*fun)(int,int )是一个带两个参数的函数指针
int(*fun)()是一个不带参数的函数指针

[此贴子已经被作者于2015-11-19 14:41编辑过]


我们都在路上。。。。。
2015-11-19 14:35
hubinyes
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:11
帖 子:104
专家分:557
注 册:2014-2-4
收藏
得分:4 
参数不匹配
2015-11-19 15:21
原以为
Rank: 2
等 级:论坛游民
帖 子:33
专家分:12
注 册:2015-10-15
收藏
得分:0 
回复 3楼 hellovfp
你运行一下第二个程序,虽然是
 void process( int x, int y, int(*fun)() );
   
却能运算出正确结果。
2015-11-19 15:39
原以为
Rank: 2
等 级:论坛游民
帖 子:33
专家分:12
注 册:2015-10-15
收藏
得分:0 
回复 3楼 hellovfp
/*函数指针*/
#include<stdio.h>                                
int max(int x,int y);                                //函数声明
int min(int x,int y);                                //函数声明
int add(int w,int z);                                //函数声明
void process(int x,int y,int(*fun)() );       //函数声明
void main()
{
    int a,b;
    printf("请输入两个数字:");
    scanf("%d %d",&a,&b);
   
   
    process(a,b, max);
    process(a,b, min);
    process(a,b, add);
}

int max(int x,int y)
{   
    int  w;
   
    w=x>y?x:y;
    return w;
}
int min(int x,int y)
{   
    int  w;
   
    w=x<y?x:y;
    return w;
}
int add(int w,int z)
{
          int x;
          x=w+z;
          return x;
}
void process(int x,int y,int (*fun)())
{
          int result;
          result=(*fun)(x,y);
          printf("%d\n", result);
         
         
}


上面这个为何错误????













#include <stdio.h>
      int max(int x, int y);            /* 函数声明 */
      int min(int x, int y);            /* 函数声明 */
      int add(int x, int y);            /* 函数声明 */  
      void process( int x, int y, int(*fun)() );    /* 函数声明 */
      

void main()
{
     
      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);
}


上面这个为何能运行????
2015-11-19 15:41
hacker梦魇
Rank: 2
等 级:论坛游民
帖 子:40
专家分:42
注 册:2015-10-29
收藏
得分:4 
两个参数的函数指针int(*fun)(int,int ) 另一个是没有的参数的函数int(*fun)(),
至于你说第一个程序为什么不能运行,那就是奇怪了,我的为什么会可以的呢?
2015-11-19 17:58
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
以下是引用原以为在2015-11-19 15:39:36的发言:

你运行一下第二个程序,虽然是
 void process( int x, int y, int(*fun)() );
   
却能运算出正确结果。


VC下面不能编译成功,何来运行出正确的结果?
--------------------Configuration: test11 - Win32 Debug--------------------
Compiling...
test11.cpp
F:\VC6_prg\test11\test11.cpp(59) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
        None of the functions with this name in scope match the target type
F:\VC6_prg\test11\test11.cpp(62) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
        None of the functions with this name in scope match the target type
F:\VC6_prg\test11\test11.cpp(65) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(void)'
        None of the functions with this name in scope match the target type
F:\VC6_prg\test11\test11.cpp(111) : error C2197: 'int (__cdecl *)(void)' : too many actual parameters
Error executing cl.exe.

test11.obj - 4 error(s), 0 warning(s)

我们都在路上。。。。。
2015-11-19 18:11
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
以下是引用原以为在2015-11-19 15:41:38的发言:

/*函数指针*/
#include<stdio.h>                                
int max(int x,int y);                                //函数声明
int min(int x,int y);                                //函数声明
int add(int w,int z);                                //函数声明
void process(int x,int y,int(*fun)() );       //函数声明
void main()
{
    int a,b;
    printf("请输入两个数字:");
    scanf("%d %d",&a,&b);
   
   
    process(a,b, max);
    process(a,b, min);
    process(a,b, add);
}

int max(int x,int y)
{   
    int  w;
   
    w=x>y?x:y;
    return w;
}
int min(int x,int y)
{   
    int  w;
   
    w=x<y?x:y;
    return w;
}
int add(int w,int z)
{
          int x;
          x=w+z;
          return x;
}
void process(int x,int y,int (*fun)())
{
          int result;
          result=(*fun)(x,y);
          printf("%d\n", result);
         
         
}


上面这个为何错误????













#include <stdio.h>
      int max(int x, int y);            /* 函数声明 */
      int min(int x, int y);            /* 函数声明 */
      int add(int x, int y);            /* 函数声明 */  
      void process( int x, int y, int(*fun)() );    /* 函数声明 */
      

void main()
{
     
      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);
}


上面这个为何能运行????


然后又跑到GCC下面编译了一下,很高兴的告诉你,
你的这段程序在GCC下面能够编译成功并运行。不知道你用的哪个编译器。
为了代码两边都能正确的编译,还是按规矩写代码吧。

ps:要是GCC后面的版本也能这样爽,我们都可以大玩函数多态了。

[此贴子已经被作者于2015-11-19 18:22编辑过]


我们都在路上。。。。。
2015-11-19 18:16
hacker梦魇
Rank: 2
等 级:论坛游民
帖 子:40
专家分:42
注 册:2015-10-29
收藏
得分:0 
C语言都是从上而下编绎的,  我用WIN-TC编绎是这样的 不过没有出现错误  输入两个数 首先从 最大值到最小值 最后两个数才相加,我看得有点郁闷
2015-11-19 18:29
快速回复:函数指针void process(int x,int y,int(*fun)(int,int ) )和void proc ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017724 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved