#include<stdio.h>
typedef void (*intFunc)(int i); // Function pointer
void test1(int age) {
printf("test1:%d
",age);
}
void foreachNums(int *nums,int len,intFunc func) {
int i;
for(i=0; i<len; i++) {
int num = nums[i];
func(num); // call the function through its pointer
}
}
void printNum(int num) {
printf("value=%d
",num);
}
int main() {
// 声明一个intFunc类型的函数指针
intFunc f1 = test1;
// 执行f1函数指针所指向的代码区
f1(8);
int nums[] = { 1,5,666,23423,223 };
foreachNums(nums,sizeof(nums)/sizeof(int),printNum); // send the function as pointer
}
运行结果: