C语言--函数指针与函数指针数组

C语言--函数指针与函数指针数组

https://blog.csdn.net/u011266694/article/details/52973573

原创 2016年10月30日 19:30:43
[cpp] view plain copy
 
  1. #include <stdio.h>  
  2. //函数指针   
  3. int fun(void)  
  4. {  
  5.     printf("Hello World\n");  
  6.     return 0;  
  7. }  
  8.   
  9. int main(int argc, const char *argv[])  
  10. {  
  11.     printf("main = %p\n",main);  
  12.     int (*p)(void) = fun;           //p     函数指针:本质是指针                                
  13.     p();                            //fun   地址常量  
  14.     return 0;  
  15. }  

 

 

[cpp] view plain copy
 
  1. #include <stdio.h>  
  2. //函数指针数组   
  3.   
  4. //函数sub   
  5. int sub(int a, int b)  
  6. {  
  7.     printf("sub = %d\n",a-b);  
  8.     return 0;  
  9. }  
  10. //函数add   
  11. int add(int a, int b)  
  12. {  
  13.     printf("add = %d\n",a+b);  
  14.     return 0;  
  15. }  
  16. int main(int argc, const char *argv[])  
  17. {  
  18.     //定义一个 函数指针数组  :其调用返回值为int ,参数为int,int   
  19.     int (*p[2]) (int , int); //本质:数组,元素:函数指针类型                                                
  20.     p[0] = sub;  
  21.     p[1] = add;  
  22.   
  23.     p[1](10,3);  
  24.     p[0](10,3);  
  25.     return 0;  
  26. }  
#include <stdio.h>  
//函数指针   
int fun(void)  
{  
    printf("Hello World!!!\n");  
    return 0;  
}  

int bye(void)  
{  
    printf("Byebye World~~\n");  
    return 0;  
} 

int main(int argc, const char *argv[])  
{  
    int (*p[2])(void) ;
    p[0]= fun;
    p[1]= bye;
    printf("main = %p\n",main);  
                                 
    p[0]();  //fun   地址常量  
    p[1]();
    return 0;  
}

/*
main = 0040100A
Hello World!!!
Byebye World~~
Press any key to continue
*/

 

 
posted @ 2018-03-28 16:10  sky20080101  阅读(96)  评论(0)    收藏  举报