c语言 函数指针使用
#include <stdio.h> //函数声明 int sum(int, int); int product(int, int); int different(int, int); //c语言就是由许多函数组成的,其中main是执行的起点,函数之间互相调用 int main() { printf("函数指针使用方法:\n"); int a = 10, b = 5; int result = 0; int (*pfun)(int, int); //声明一个函数指针 pfun = sum; //指针传递 result = pfun(a, b); printf("result: %d\n", result); pfun = different; result = pfun(a, b); printf("result: %d\n", result); pfun = product; result = pfun(a, b); printf("result: %d\n", result); printf("\n"); //函数指针数组 int (*pfuns[3])(int, int); //声明一个函数指针数组 指针是一个变量,函数指针可以像其他变量一样使用 pfuns[0] = sum; pfuns[1] = different; pfuns[2] = product; int i = 0; while (i < 3) { result = pfuns[i](a, b); printf("result: %d\n", result); i++; } return 0; } int sum(int x, int y) { return x + y; } int product(int x, int y) { return x * y; } int different(int x, int y) { return x - y; }
相关推荐
拉斯厄尔高福 2020-10-19
徐建岗网络管理 2020-06-26
penkgao 2020-06-25
fengjing81 2020-06-24
penkgao 2020-10-13
lsfreeing 2020-06-20
Biao 2020-06-17
yunfeitian 2020-06-13
AaronPlay 2020-06-13
chensen 2020-11-14
拉斯厄尔高福 2020-11-04
杜倩 2020-10-29
嵌入式资讯精选 2020-10-15
zhaochen00 2020-10-13
yiyilanmei 2020-10-05
wanshiyingg 2020-09-29
Mars的自语 2020-09-27
shenwenjie 2020-09-24
一个逗逗 2020-09-22