C语言函数指针的几种用法【转】+gyy修改

来源:互联网 发布:淘宝猫粮成分分析 编辑:程序博客网 时间:2024/06/08 15:24

 

C语言函数指针的几种用法---gyy修改后

程序如下:

#include <iom32.h>

#include <stdio.h>

 

/*--指针的应用都在主函数里,前面都是准备工作--*/

 

/*----------------------------------------------------------*/

/*------简单应用指针的前期准备------*/

/*----------------------------------------------------------*/

//声明并定义一个变量、一个数组一个函数

double R = 0; 

 

unsigned char Temp[10] = {9,8,7,6,5,4,3,2,1,0};

 

void delay_ms(unsigned int n) //类似时间延迟作用

{

 unsigned int i,j;

 for(i = 0; i< n ; i++)

 {

   for(j = 0; j < 2280; j++);//循环体为空,不做具体操作,循环制作控制变量自身变化

 }

}

 

//声明一个指向无符号字符型的指针一个函数指针

unsigned char *pT; 

 

void (*pF)(unsigned int n);

 //声明函数指针,该函数指针指向带有一个无符号整型参数并且类型为void的函数

 

/*----------------------------------------------------------*/

/*------应用函数指针的前期准备------*/

/*----------------------------------------------------------*/

 

/*------相关函数的声明和定义------*/

int add(int a, int b) //加法函数,返回两个参数和

{int sum;       sum = a + b;       return sum;}

int sub(int a, int b)  //减法函数,返回两个参数差

{int difference; difference = a - b; return difference;}

int mul(int a, int b)//乘法函数,返回两个参数乘积

{int product;   product = a * b;   return product;}

int div(int a, int b)//除法函数,返回两个参数商

{int division;  division = a / b;  return division;}

int UnKnown(int a,int b){return 0;}//函数值为0

 

/*------函数指针的第一种用法:作为其他函数的参数------*/

//关于下面这个函数的理解:首先分析这个函数的参数,前两个参数是整形,

//                       第三个参数是一个函数指针,既然是函数指针,

//                       那么给第三个参数赋值时,

//                       就应该是函数的入口地址,即函数名;

//                       调用时:calculator(5,9,mul);

//

//函数的声明和定义

double calculator1(int x, int y, int (*pfunc)(int, int))

{

 double result;

 result = (*pfunc)(x, y); //调用函数指针所指函数的操作值

 return result;

}

 

 

/*------函数指针的第二种用法:作为函数的返回值------*/

//直接声明和定义(思路不清晰,不推荐)

//该函数形参为unsigned char c

//该函数的返回值类型为函数指针 int (*GetOperation1)(int a,int b)

int (*GetOperation1(unsigned char c))(int a,int b)                    

{

 switch (c)

 {

 case 'a':

   return add;

 case 's':

   return sub;

 case 'm':

   return mul;

 case 'd':

   return div;

 default:

   return UnKnown;

 }

}

//标准做法(思路清晰,推荐)

//PF转意声明成一个指向int add(int a, int b)型函数的函数指针类型

typedef int (*PF)(int ,int);//为函数指针类型起类型名,PF为指向含有两个整型参数并且返回值为整数的函数指针类型

 

//利用 PF 声明定义一个返回值为指向int add(int a, int b)型函数指针的函数

PF GetOperation2(unsigned char c)

{

 switch (c)

 {

 case 'a':

   return add;

 case 's':

   return sub;

 case 'm':

   return mul;

 case 'd':

   return div;

 default:

   return UnKnown;

 }

}

 

/*------函数指针的第三种用法:函数指针数组(即所谓的函数指针表)------*/

 

/*------简单指针表实现过程:适用于简单逻辑------*/

//直接定义一个函数指针数组;

//函数指针数组中每一个元素都为一个函数指针指向某一个函数

int (*calculator2[4])(int , int ) =

{

 add,sub,mul,div

};

 

/*------标准指针表实现过程:适用于复杂的逻辑------*/

//第一步:定义一些相关的位  

#define X_bit_0 (1<<0)//

#define X_bit_1 (1<<1)//

#define X_bit_2 (1<<2)//

#define X_bit_3 (1<<3)//

//第二步:声明和实现指针表里要用的函数

//前面已定义

/*

int add(int a, int b) //加法函数,返回两个参数和

{int sum;       sum = a + b;       return sum;}

int sub(int a, int b)  //减法函数,返回两个参数差

{int difference; difference = a - b; return difference;}

int mul(int a, int b)//乘法函数,返回两个参数乘积

{int product;   product = a * b;   return product;}

int div(int a, int b)//除法函数,返回两个参数商

{int division;  division = a / b;  return division;}

int UnKnown(int a,int b){return 0;}//函数值为0

 */

//第三步:定义一个此种类型的结构体:

//       X_bit是个标识,(*Calc)()是当标志有效时要执行的动作

typedef struct{          

 unsigned char X_bit;

 int (*Calc)(int,int);

}Example;

//第四步:建立函数指针表

Example Calcultor[4]={   

 {X_bit_0,add},{X_bit_1,sub},{X_bit_2,mul},{X_bit_3,div}

};

 

 

 

void main()

{

 /*------简单的指针应用------*/

 //给指针赋值

 pT = Temp;   // pT = &Temp[0];也可以

 pF = delay_ms;// F = &delay_ms;也可以

 

  //引用指针  

 /*-自我约束:为了程序的可读性,在引用函数指针时,一律用 (*pF)(形参表)的格式-*/

 R = *(pT+2); //指向数组的

 (*pF)(5000);  //指向函数的

 

  /*------函数指针的应用------*/ 

 //基本应用:用指针来调用函数

 (*pF)((unsigned int)R);   

 

  //函数指针的第一种用法:将函数指针作为参数

 R = calculator1(3, 5, sub);

 

  //函数指针的第二种用法:将函数指针作为函数的返回值

 unsigned char Op = 's';//这个变量的值应该是外面传进来的,如中断等 

 //简单的

 R = (*GetOperation1(Op))(4,3); 

 //标准的

 R = (*GetOperation2(Op))(5,7);

   

 //函数指针的第三种用法:函数指针表

 //简单函数指针表的应用 

 R = (*calculator2[0])(3, 5);

 

 //标准函数指针表的应用

 unsigned char Type_Of_Calc = 2;//这个变量应该是外面传进来的,如中断等

 for(unsigned char i = 0; i < 4; i++)

 {

   if( (Type_Of_Calc == (Calcultor[i].X_bit))

      && (Calcultor[i].Calc != NULL) )

   {

     R = (*Calcultor[i].Calc)(6,4);

   }

 }

}

 

 

//C语言函数指针的几种用法---gyy修改后#include <iostream.h>#include <stdio.h>//声明并定义 一个变量、一个数组 和 一个函数double R = 0;  unsigned char Temp[10] = {9,8,7,6,5,4,3,2,1,0};void delay_ms(unsigned int n)  //类似时间延迟作用{  unsigned int i,j;  for(i = 0; i< n ; i++)  {    for(j = 0; j < 2280; j++); //循环体为空,不做具体操作,循环制作控制变量自身变化  }}//声明一个指向无符号字符型的指针 和 一个函数指针unsigned char *pT;  void (*pF)(unsigned int n);//声明函数指针,该函数指针指向带有一个无符号整型参数并且类型为void的函数/*--指针的应用都在主函数里,前面都是准备工作--*//*----------------------------------------------------------*//*------简单应用指针的前期准备------*//*----------------------------------------------------------*//*----------------------------------------------------------*//*------应用函数指针的前期准备------*//*----------------------------------------------------------*//*------相关函数的声明和定义------*/int add(int a, int b)  //加法函数,返回两个参数和{int sum;        sum = a + b;     cout<<a<<"+"<<b<<"=" ;   return sum;}int sub(int a, int b)   //减法函数,返回两个参数差{int difference; difference = a - b; cout<<a<<"-"<<b<<"=" ; return difference;}int mul(int a, int b) //乘法函数,返回两个参数乘积{int product;    product = a * b;    cout<<a<<"*"<<b<<"=" ;return product;}int div(int a, int b) //除法函数,返回两个参数商{int division;   division = a / b;   cout<<a<<"/"<<b<<"=" ;return division;}int UnKnown(int a,int b){return 0;} //函数值为0/*------函数指针的第一种用法:作为其他函数的参数------*///关于下面这个函数的理解:首先分析这个函数的参数,前两个参数是整形,//                        第三个参数是一个函数指针,既然是函数指针,//                        那么给第三个参数赋值时,//                        就应该是函数的入口地址,即 函数名;//                        调用时:calculator(5,9,mul);////函数的声明和定义double calculator1(int x, int y, int (*pfunc)(int, int)) {  double result;  result = (*pfunc)(x, y);  //调用函数指针所指函数的操作值  return result;}/*------函数指针的第二种用法:作为函数的返回值------*///直接声明和定义(思路不清晰,不推荐)//该函数形参为unsigned char c//该函数的返回值类型为函数指针  int (*GetOperation1)(int a,int b)int (*GetOperation1(unsigned char c))(int a,int b)    {  switch (c)  {  case 'a':    return add;  case 's':    return sub;  case 'm':    return mul;  case 'd':    return div;  default:    return UnKnown;  }}//标准做法(思路清晰,推荐)//将PF转意声明成一个指向int add(int a, int b)型函数的函数指针类型typedef int (*PF)(int ,int); //为函数指针类型起类型名,PF为指向含有两个整型参数并且返回值为整数的函数指针类型//利用 PF 声明定义一个返回值为 指向int add(int a, int b)型函数指针 的函数PF GetOperation2(unsigned char c){  switch (c)  {  case 'a':    return add;  case 's':    return sub;  case 'm':    return mul;  case 'd':    return div;  default:    return UnKnown;  }}/*------函数指针的第三种用法:函数指针数组(即所谓的函数指针表)------*/ /*------简单指针表实现过程:适用于简单逻辑------*///直接定义一个函数指针数组;//函数指针数组中每一个元素都为一个函数指针指向某一个函数int (*calculator2[4])(int , int ) ={  add,sub,mul,div};/*------标准指针表实现过程:适用于复杂的逻辑------*///第一步:定义 一些相关的位   #define X_bit_0 (1<<0)//加#define X_bit_1 (1<<1)//减#define X_bit_2 (1<<2)//乘#define X_bit_3 (1<<3)//除//第二步:声明和实现指针表里要用的函数/*int add(int a, int b)  //加法函数,返回两个参数和{int sum;        sum = a + b;        return sum;}int sub(int a, int b)   //减法函数,返回两个参数差{int difference; difference = a - b; return difference;}int mul(int a, int b) //乘法函数,返回两个参数乘积{int product;    product = a * b;    return product;}int div(int a, int b) //除法函数,返回两个参数商{int division;   division = a / b;   return division;}int UnKnown(int a,int b){return 0;} //函数值为0*///第三步:定义一个此种类型的结构体://        X_bit是个标识,(*Calc)()是当标志有效时要执行的动作typedef struct{             unsigned char X_bit;  int (*Calc)(int,int);}Example;//第四步:建立函数指针表Example Calcultor[4]={      {X_bit_0,add},{X_bit_1,sub},{X_bit_2,mul},{X_bit_3,div}};void main(){  /*------简单的指针应用------*/  //给指针赋值  pT = Temp;    // pT = &Temp[0];也可以  pF = delay_ms; // F = &delay_ms;也可以    //引用指针     /*-自我约束:为了程序的可读性,在引用函数指针时,一律用 (*pF)(形参表)的格式-*/  R = *(pT+2);  //指向数组的  cout<<"当前R的值为:"<<R<<endl;  cout<<"通过当前函数指针调用延迟函数"<<endl;  (*pF)(5000);   //指向函数的   /*------函数指针的应用------*/    //基本应用:用指针来调用函数  (*pF)((unsigned int)R) ;    //函数指针的第一种用法:将函数指针 作为参数  cout<<"通过函数指针作为函数参数进行运算并得到结果  ";  R = calculator1(3, 5, sub);  cout<<R<<endl;    //函数指针的第二种用法:将函数指针 作为函数的返回值  unsigned char Op = 's';//这个变量的值应该是外面传进来的,如中断等    //简单的  cout<<"通过简单返回函数指针的函数调用进行运算并得到结果  ";  R = (*GetOperation1(Op))(4,3);    cout<<R<<endl;  //标准的  cout<<"通过标准返回函数指针的函数调用进行运算并得到结果  ";  R = (*GetOperation2(Op))(5,7);   cout<<R<<endl;      //函数指针的第三种用法:函数指针表  //简单函数指针表的应用    cout<<"通过简单函数指针表进行操作并得到结果  ";  R = (*calculator2[0])(3, 5);  cout<<R<<endl;    //标准函数指针表的应用  unsigned char Type_Of_Calc = 2;//这个变量应该是外面传进来的,如中断等  for(unsigned char i = 0; i < 4; i++)  {    if( (Type_Of_Calc == (Calcultor[i].X_bit))       && (Calcultor[i].Calc != NULL) )    {       cout<<"通过标准函数指针表进行操作并得到结果  ";   R = (*Calcultor[i].Calc)(6,4);   cout<<R<<endl;    }  }}


 

原创粉丝点击