C语言--函数指针与函数指针数组
C语言--函数指针与函数指针数组
https://blog.csdn.net/u011266694/article/details/52973573
- #include <stdio.h>
- //函数指针
- int fun(void)
- {
- printf("Hello World\n");
- return 0;
- }
- int main(int argc, const char *argv[])
- {
- printf("main = %p\n",main);
- int (*p)(void) = fun; //p 函数指针:本质是指针
- p(); //fun 地址常量
- return 0;
- }
- #include <stdio.h>
- //函数指针数组
- //函数sub
- int sub(int a, int b)
- {
- printf("sub = %d\n",a-b);
- return 0;
- }
- //函数add
- int add(int a, int b)
- {
- printf("add = %d\n",a+b);
- return 0;
- }
- int main(int argc, const char *argv[])
- {
- //定义一个 函数指针数组 :其调用返回值为int ,参数为int,int
- int (*p[2]) (int , int); //本质:数组,元素:函数指针类型
- p[0] = sub;
- p[1] = add;
- p[1](10,3);
- p[0](10,3);
- return 0;
- }
#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 */

浙公网安备 33010602011771号