c语言技巧二

马上要离职了,闲着没事,测试几个c语言技巧:


指针数组

函数指针

函数指针数组


#include<stdio.h>


#define    test_arg 0

struct    funs_group//定义指针结构体

{
    void (*ptr1)(void);
    void (*ptr2)(void);
    void (*ptr3)(void);
    void (*ptr4)(void);
    
};
typedef    struct funs_group ptr_group;//typedef 一下
int max(int x,int y)//大小比较函数
{
    return    ( x>y? x:y);
}

void func1(void)//测试功能函数
{
    printf("we are in function:%s\n",__FUNCTION__);
}


void func2(void)//测试功能函数
{
    printf("we are in function:%s\n",__FUNCTION__);
}


void func3(void)//测试功能函数
{
    printf("we are in function:%s\n",__FUNCTION__);
}


void func4(void)//测试功能函数
{
    printf("we are in function:%s\n",__FUNCTION__);
}

int main(void)
{
    int (*ptr)(int,    int);//定义函数指针
    int a,b,c;
    int i,j;
    void ((*larry[4])(void))={func1,func2,func3,func4};//初始化函数指针数组
    ptr_group        my_func;//定义函数指针结构体
    ptr=max;//对函数指针赋值
    
    scanf("%d%d",&a,&b);

    c=(*ptr)(a,b);//执行
    
    printf("a=%d,b=%d,c=%d\n",a,b,c);
//赋值
    my_func.ptr1=func1;
    my_func.ptr2=func2;
    my_func.ptr3=func3;
    my_func.ptr4=func4;
//执行
    (*(my_func.ptr1))();
    (*(my_func.ptr2))();
    (*(my_func.ptr3))();
    (*(my_func.ptr4))();

//获取数组大小

   j=(sizeof(larry))    /(sizeof(larry[0]));

    printf("larry num is %d! \n",j);

执行

    for(i=0;    i<j;    i++)

    {
        (*(larry[i]))();
    }
    
    return 0;
}

你可能感兴趣的:(c,function,struct,测试,语言)