首页 > 代码库 > c语言之函数指针的一系列测试
c语言之函数指针的一系列测试
1.先从一个简单的求和求积函数开始
#include <stdio.h> int add(int a , int b) { return a + b; } int mul(int a , int b) { return a * b; } int main() { int a_count = add(5,7); int m_count = mul(5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0; }
输出:
a_count is 12
m_count is 35
Program ended with exit code: 0
2.试一下函数指针
#include <stdio.h> int add(int a , int b) { return a + b; } int mul(int a , int b) { return a * b; } int main() { int (*p_add)(int,int);//声明函数指针 p_add = add; int (*p_mul)(int,int); p_mul = mul; int a_count = p_add(5,7); int m_count = p_mul(5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0; }
结果不变
a_count is 12
m_count is 35
Program ended with exit code: 0
3.简化一下函数指针的定义
#include <stdio.h> int add(int a , int b) { return a + b; } int mul(int a , int b) { return a * b; } int main() { typedef int (*fun)(int,int);//定义类型fun是一个指向函数的指针 fun p_add = add; fun p_mul = mul; int a_count = p_add(5,7); int m_count = p_mul(5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0; }
结果不变
a_count is 12
m_count is 35
Program ended with exit code: 0
4.尝试使用一下函数指针数组
#include <stdio.h> int add(int a , int b) { return a + b; } int mul(int a , int b) { return a * b; } int main() { int (*func[])(int,int) = {add,mul};//定义函数指针数组 int a_count = func[0](5,7); int m_count = func[1](5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0; }
结果不变
a_count is 12
m_count is 35
Program ended with exit code: 0
5.改善函数指针数组
毕竟,函数多了之后,谁能记住乘法是在数组里第几个,下标该是多少。
#include <stdio.h> int add(int a , int b) { return a + b; } int mul(int a , int b) { return a * b; } int main() { int (*func[])(int,int) = {add,mul};//定义函数指针数组 enum func_tpye{ ADD , MUL }; //顺序与上面保持一致 int a_count = func[ADD](5,7); int m_count = func[MUL](5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0; }
结果不变
a_count is 12
m_count is 35
Program ended with exit code: 0
6.在函数中调不同的函数
之前的例子,直接用函数会更好用一些
#include <stdio.h> typedef int (*fun)(int,int); int add(int a , int b) { return a + b; } int mul(int a , int b) { return a * b; } int add_mul(fun f,int a,int b) { return f(a,b); } int main() { int (*func[])(int,int) = {add,mul};//定义函数指针数组 enum func_tpye{ ADD , MUL }; //顺序与上面保持一致 printf("a_count is %d\n",add_mul(func[ADD], 5, 7)); printf("m_count is %d\n",add_mul(func[MUL], 5, 7)); return 0; }
结果不变
a_count is 12
m_count is 35
Program ended with exit code: 0
7.上面那个例子还可以再扩展的更强大一些
比如说把两个数相乘相加改为多个数相乘相加
#include <stdio.h> #include <stdarg.h> typedef int (*fun)(int,int); int add(int a , int b) { return a + b; } int mul(int a , int b) { return a * b; } int add_mul(fun f,int n,...) { va_list ap; va_start(ap,n); int count = va_arg(ap, int); for (int i = 0; i<(n-1); ++i) { count = f(count,va_arg(ap, int)); } return count; } int main() { int (*func[])(int,int) = {add,mul};//定义函数指针数组 enum func_tpye{ ADD , MUL }; //顺序与上面保持一致 printf("a_count is %d\n",add_mul(func[ADD], 5,1,2,3,4,5)); printf("a_count is %d\n",add_mul(func[ADD], 3,7,3,6)); printf("m_count is %d\n",add_mul(func[MUL], 5,1,2,3,4,5)); printf("m_count is %d\n",add_mul(func[MUL], 3,7,3,6)); return 0; }
第一个参数为功能函数,第二个参数为运算的个数,之后是不定参数。
结果为:
a_count is 15
a_count is 16
m_count is 120
m_count is 126
Program ended with exit code: 0
c语言之函数指针的一系列测试
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。