码迷,mamicode.com
首页 > 编程语言 > 详细

c语言之函数指针的一系列测试

时间:2014-09-09 12:32:48      阅读:321      评论:0      收藏:0      [点我收藏+]

标签: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语言之函数指针的一系列测试

标签:c语言   函数指针   测试   

原文地址:http://blog.csdn.net/wangxiaobupt/article/details/39153325

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!