函数指针例子说明

来源:互联网 发布:kindle 安装软件 编辑:程序博客网 时间:2024/05/03 03:35

http://blog.csdn.net/sruru/article/details/7916296

在C/C++中存在着函数指针,即指向函数的指针。我目前已知的两种使用方法是:

  

[cpp] view plaincopy
  1. #include <string.h>  
  2. #include <stdio.h>  
  3. typedef int* PINNT;  
  4. #define PP int*  
  5.   
  6. int funcA(int a,int b);  
  7. int funcB(int* a,int *b);  
  8. int  main(int argc, char *argv[])  
  9. {  
  10.     int (*func)(int,int);  
  11.     //func = &funcA;  
  12.     func = funcA;  
  13.     //两种赋值给函数指针的方法都可以  
  14.     printf("%d",func(1,10));  
  15.     //printf("%d",(*func)(1,10));  
  16.     //两种调用函数指针的方法都可以  
  17.     //两种赋值方法和两种调用方法可以任选一种组合  
  18.       
  19. }  
  20.   
  21. int funcA(int a,int b)  
  22. {  
  23.     return a + b;  
  24. }  
  25.   
  26. int funcB(int* a,int *b)  
  27. {  
  28.     (*a) = (*a) + (*b);  
  29.     return 1;  
  30. }  


 

这里int (*func)(int,int)定义了一个函数指针变量,可以接受函数名或者函数名的指针(这里我还没有很清楚)。

 

然后我在CSDN上面找到了一个例子,据说是华为的面试题目,代码如下,这时程序正常输出值为110。

[cpp] view plaincopy
  1. #include <stdio.h>   
  2. int   inc(int   a)   
  3. {   
  4.     return(++a);   
  5. }   
  6.   
  7. int   multi(int*a,int*b,int*c)   
  8. {   
  9.     return(*c=*a**b);   
  10. }   
  11.   
  12. typedef   int(FUNC1)(int);   
  13. typedef   int(FUNC2)(int*,int*,int*);   
  14.   
  15. void   show(FUNC2   fun,int   arg1,   int*arg2)   
  16. {   
  17.     FUNC1   *p = &inc;   
  18.     int   temp   =p(arg1);   
  19.     fun(&temp,&arg1,   arg2);   
  20.     printf( "%d\n ",*arg2);   
  21. }   
  22.   
  23. main()   
  24. {   
  25.     int   a;   
  26.     show(multi,10,&a);   
  27.     getchar();   
  28.     return   0;   
  29. }  

一开始我没有理解这个用法的时候,以为这里使用的方法与方法一相同,故直接写成


[cpp] view plaincopy
  1. typedef   int(FUNC1)(int);   
  2. typedef   int(FUNC2)(int*,int*,int*);   
  3.   
  4. void   show(FUNC2   fun,int   arg1,   int*arg2)   
  5. {   
  6.     FUNC1  = inc;   
  7.     int   temp   =p(arg1);   
  8.     fun(&temp,&arg1,   arg2);   
  9.     printf( "%d\n ",*arg2);   
  10. }   


这时候,编译器给出的提示是:

1>d:\vctest\test4\test4\test4.c(17) : error C2513: 'int (int)' : no variable declared before '=' 

FUNC1 = inc这个语句的左边没有变量被申明,这个错误提示说明利一点:FUNC是一种类型而不是一个变量。做出如下更正:

 

[html] view plaincopy
  1. FUNC1  *p = inc;   


程序正确,更加证明了

[cpp] view plaincopy
  1. typedef   int(FUNC1)(int);   
  2. typedef   int(FUNC2)(int*,int*,int*);   

语句定义了两个自己的数据类型FUNC1和FUNC2,这两个类型申明的变量用保存函数指针

 

 也可以采用下面的方式进行定义:

[cpp] view plaincopy
  1. typedef   int (*FUNC1)(int);   
  2. typedef   int (FUNC2)(int*,int*,int*);   
  3.   
  4. void   show(FUNC2   fun,int   arg1,   int*arg2)   
  5. {   
  6.     FUNC1  p = inc;   
  7.     int   temp   = p(arg1);   
  8.     fun(&temp,&arg1,   arg2);   
  9.     printf( "%d\n ",*arg2);   
  10. }   
  11.    

 

以上例子,涉及到的概念有函数指针,函数类型和函数指针变量

函数指针变量有两种实现方法:

    1. 先申明函数指针类型,在用函数指针类型申明函数指针变量

    //typedef void FunType(int);  -- FunType:同样是一个函数指针类型。

[cpp] view plaincopy
  1. typedef void (*PF)(int,int);  
  2. //使用函数指针类型申明的变量就是函数指针变量  
  3. //PF a = &inc//PF a = inc  

    2. 直接定义一个函数指针变量

 

[cpp] view plaincopy
  1. void (*fPV)(int,int);  
  2. fvp  = inc // fvp = &inc  


函数类型与函数指针变量

    如何定义函数类型呢

   

[cpp] view plaincopy
  1. typedef void (FT)(int,int);  

    申明的函数类型如何使用呢?

 

首先可以用来申明函数:

FT x,y等价于:

void x(int,int);

void y(int,int);

请看如下的示例:

[cpp] view plaincopy
  1. #include <stdio.h>   
  2.   
  3. typedef int (FT)(int*,int*);  
  4. FT x,y;  
  5.   
  6.   
  7. main()   
  8. {   
  9.     int a = 1,b=2;  
  10.     x(&a,&b);  
  11.     (*y)(&a,&b);  
  12. }  
  13.   
  14. int x(int* a,int*b){  
  15.     *a = *a + *b;  
  16.     printf("%d\n",*a);  
  17. }  
  18.   
  19. int y(int*a,int* b){  
  20.     *a = *a * (*b);  
  21.     printf("%d\n",*a);  
  22. }  


 

上面的程序输出3和6

 

[cpp] view plaincopy
  1. #include <stdio.h>   
  2.   
  3. typedef int (FT)(int*,int*);  
  4. FT x,y,*z,u;  
  5.   
  6.   
  7. main()   
  8. {   
  9.     int a = 1,b=2;  
  10.     x(&a,&b);  
  11.     (*y)(&a,&b);  
  12.     z = x;//这里x是一个函数名,但是编译器会把函数名转换为函数指针变量,所以也可以显式写成z = &x;  
  13.     x(&a,&b);  
  14.     //u = x;//这里出错了这里u是一个函数名称,是一个指针常量,类似于数组名称,是不能够被赋值的  
  15. }  
  16.   
  17. int x(int* a,int*b){  
  18.     *a = *a + *b;  
  19.     printf("%d\n",*a);  
  20. }  
  21.   
  22. int y(int*a,int* b){  
  23.     *a = *a * (*b);  
  24.     printf("%d\n",*a);  
  25. }  


上面的例子中FT *Z其实是申明了一个指向函数的函数指针变量,类似于int(*z)(int*,int*)。

 

最后再来看下那个困扰了我很久的例子吧:

[cpp] view plaincopy
  1. #include <stdio.h>   
  2. int   inc(int   a)   
  3. {   
  4.     return(++a);   
  5. }   
  6.   
  7. int   multi(int*a,int*b,int*c)   
  8. {   
  9.     return(*c=*a**b);   
  10. }   
  11.   
  12. typedef   int (*FUNC1)(int);   
  13. typedef   int (FUNC2)(int*,int*,int*);   
  14. typedef   int (FUNC3)(int*,int*,int*);   
  15.   
  16. void   show(FUNC2 *fun,int   arg1,   int*arg2)   
  17. //void   show(FUNC2 fun,int   arg1,   int*arg2)//这里做这样的声明也一样是对的   
  18. {   
  19.     FUNC1  p = &inc;   
  20.     FUNC2 *q = multi;  
  21.     int   temp   = (*p)(arg1);   
  22.     (*fun)(&temp,&arg1,   arg2);  
  23.     //fun(&temp,&arg1,   arg2); //这里的两种调用方法和上面的恶两种声明方法可以任意组合,,原因是函数名和函数指针常量的隐式转换  
  24.     printf( "%d\n ",*arg2);   
  25. }   
  26.   
  27. main()   
  28. {   
  29.     int   a;   
  30.     show(multi,1,&a);   
  31.     getchar();   
  32.     return   0;   
  33. }  

最让我疑惑的是

void   show(FUNC2 *fun,int   arg1,   int*arg2) 
//void   show(FUNC2 fun,int   arg1,   int*arg2)//这里做这样的声明也一样是对的

这两种声明方式都是对的,采用void   show(FUNC2 fun,int   arg1,   int*arg2)//这里做这样的声明也一样是对的这种方式声明, show(multi,1,&a);调用时正确的,

而前面的代码中,我也说过//u = x;//这里出错了这里u是一个函数名称,是一个指针常量,类似于数组名称,是不能够被赋值的, 这个问题困扰我很久啊,仔细考虑之后,我个人的观点是:void   show(FUNC2 fun,int   arg1,   int*arg2)使用一个函数做为形式参数,调用时传递一个函数名字给他就可以,也可以传递一个函数常量指针。类比于使用指针常量或者数组作函数书参数的时候

[cpp] view plaincopy
  1. #include <stdio.h>   
  2.   
  3. typedef void (FT)(intconst a);  
  4. FT z;  
  5. void main()  
  6. {  
  7.     int a[]={1,2,3,4,5};  
  8.     int b = 5;  
  9.     int *c = &b;  
  10.     z(a);  
  11.     //但是如果在这里给a = c是不对的  
  12. }  
  13.   
  14. void z(intconst a)  
  15. {  
  16.     printf("%d",*a);  
  17. }  

 

0 0
原创粉丝点击