一个函数指针的例子--int (* FunctionFound(char op))(int ,int) ;

//此代码演示函数指针的例子,再windows下,利用gcc命令编译;
/*******************************************
 * this file is to test function pointer
 *
 *******************************************/
#include <stdio.h>


typedef int(*FP_CALL)(int,int);  //Defines a function pointer type


int (*data_return)(int);  // Defines a function pointer of  global type

int DataReturn(int a)
{
  return a;
}

int AddCaculate(int a,int b)
{
	return (a + b);
	}
	
FP_CALL CallFunction(char op)
{
	switch(op)
	{
		case '+' : 
			printf("found the function AddCaculate \n");
			return AddCaculate;
			break;
		default :
			printf("the input error\n");
		}
	}
	
int (* FunctionFound(char op))(int ,int) // this is a function whose return data is a function pointer;
{
	return CallFunction(op);
	}

void main()
{
	FP_CALL function_call;
	int *p;
	int a = 100;
	
	data_return = DataReturn;
	
	p = &a;
	
	function_call = FunctionFound('+');
	
	printf("hello my honey!!!\n");
	printf("the function pointer data_return  address  is : %d \n",data_return);
	printf("the function pointer *data_return address  is : %d \n",*data_return);
	
	printf("the (*data_return)(2) result is : %d .\n",(*data_return)(2));
	printf("the   data_return(3)  result is : %d .\n",data_return(3));

	printf("the int pointer (*p) is : %d \n",*p);
	printf("the int pointer p is : %d \n",p);
	
	printf("the function_call run result is :%d \n",function_call(10,12));
	
	/*  */
	
	}

运行结果:


分析:
  1、typedef int(*FP_CALL)(int,int);   //定义一个函数指针类型,并不是一个函数指针;
        FP_CALL function_call;   // 这一句才是定义一个函数指针;
  2、  int (*data_return)(int);    // 这个是直接定义一个函数指针;与1中,只是定义方法不同;
  3、FP_CALL CallFunction(char op)    //此函数的返回值为一个函数指针;
  4、int (* FunctionFound(char op))(int ,int)  // 此函数的返回值也是一个函数指针;此函数指针拥有两个int参数,返回值类型为int;
  5、data_return 和 *data_return 定义的地址是一样的;也就是说data_return(2);与(*data_return)(2);都可以调用函数指针所指向的函数;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamStreaking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值