今天在整理《数据结构》(陈越) 中的图时, 读到一段用到了函数指针的写法, 初时很懵, 遂谷歌之, 找到了菜鸟教程的写得很好的一篇解释, 现转载整理如下.
书中使用到了函数指针的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| void Visit(Vertex V) { printf("正在访问顶点%d\n", V); }
void DFS(LGraph Graph, Vertex V, void(*Visit)(Vertex)) { PtrToAdjVNode W;
Visit(V); Visited[V] = 1;
for (W = Graph->G[V].FirstEdge; W; W = W->Next) if (!Visited[W->AdjV]) DFS(Graph, W->AdjV, Visit); }
|
函数指针
函数指针是指向函数的指针变量。
通常我们说的指针变量是指向一个整型、字符型或数组等变量,而函数指针是指向函数。
函数指针可以像一般函数一样,用于调用函数、传递参数。
函数指针变量的声明:
1
| typedef int (*fun_ptr)(int,int);
|
实例
以下实例声明了函数指针变量 p,指向函数 max:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdio.h> int max(int x, int y) { return x > y ? x : y; } int main(void) { int (* p)(int, int) = & max; int a, b, c, d; printf("请输入三个数字:"); scanf("%d %d %d", & a, & b, & c); d = p(p(a, b), c); printf("最大的数字是: %d\n", d); return 0; }
|
输出结果:
回调函数
函数指针作为某个函数的参数
函数指针变量可以作为某个函数的参数来使用的,回调函数就是一个通过函数指针调用的函数。
简单讲:回调函数是由别人的函数执行时调用你实现的函数。
以下是来自知乎作者常溪玲的解说:
你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货。在这个例子里,你的电话号码就叫回调函数,你把电话留给店员就叫登记回调函数,店里后来有货了叫做触发了回调关联的事件,店员给你打电话叫做调用回调函数,你到店里去取货叫做响应回调事件。
实例
实例中 populate_array 函数定义了三个参数,其中第三个参数是函数的指针,通过该函数来设置数组的值。
实例中我们定义了回调函数 getNextRandomValue,它返回一个随机值,它作为一个函数指针传递给 populate_array 函数。
populate_array 将调用 10 次回调函数,并将回调函数的返回值赋值给数组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #include <stdlib.h> #include <stdio.h>
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)) { for (size_t i=0; i<arraySize; i++) array[i] = getNextValue(); }
int getNextRandomValue(void) { return rand(); } int main(void) { int myarray[10]; populate_array(myarray, 10, getNextRandomValue); for(int i = 0; i < 10; i++) { printf("%d ", myarray[i]); } printf("\n"); return 0; }
|
编译执行,输出结果如下:
1
| 16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709
|