# 【系列】C/C++函数指针详解-2

2016-12-14 09:56:02来源:http://blog.csdn.net/Hipercomer/article/details/8616709作者:Hipercomer人点击

## 3.4函数指针作为参数

`     /**Author:Choas Lee*Date:2012-02-28*/#includefloat add(float a,float b){return a+b;}float minus(float a,float b){return a-b;}float multiply(float a,float b){return a*b;}float divide(float a,float b){return a/b;}int pass_func_pointer(float (*pFunction)(float a,float b)){      float result=pFunction(10.0,12.0);      printf("result=%f/n",result);}int main(){      pass_func_pointer(add);      pass_func_pointer(minus);      pass_func_pointer(multiply);      pass_func_pointer(divide);      return 0;} `

`result=22.000000result=-2.000000result=120.000000result=0.833333 `

## 3.5使用函数指针作为返回值

int func(){}

int * func(){}

float *

float (*)(float ,float )

float (* func(char op) ) (float ,float)

l其名称为func，其参数的个数为1个；

l其各个参数的类型为：op—char

l其返回变量（函数指针）类型为：float(*)(float,float)

void (*signal (int signo,void (*func)(int) ) ) (int);

l其函数名称为：signal

l其参数个数为：2

l其各个参数的类型为：signo--int,
func— void (*)(int)

l其返回的变量（函数指针）的类型为：void(*)(int)

```/**Author:Choas Lee*Date:2012-02-28*/#include#include#includefloat add(float a,float b){return a+b;}float minus(float a,float b){return a-b;}float multiply(float a,float b){return a*b;}float divide(float a,float b){return a/b;}float(* FunctionMap(char op) )(float,float){      switch(op)      {             case '+':                    return add;                    break;             case '-':                    return minus;                    break;             case '*':                    return multiply;                    break;             case '//':                    return divide;                    break;             default:                    exit(1);      }}int main(){      float a=10,b=5;      char ops[]={'+','-','*','//'};      int len=strlen(ops);      int i=0;      float (*returned_function_pointer)(float,float);      for(i=0;i计算的结果为：the result caculated by the operator + is 15.000000the result caculated by the operator - is 5.000000the result caculated by the operator * is 50.000000the result caculated by the operator / is 2.000000
```