C语言进阶——函数指针

函数名称即是指针
1、声明函数指针注意,要声明的函数的返回值 ,参数

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void  printfX(){
    printf("xxxxxxxx\n");
    printf("xxxxxxxx\n");
    printf("xxxxxxxx\n");
    printf("xxxxxxxx\n");
}

int main(void){


    printf("打印printfX = %p\n",printfX);
    printf("打印&printfX = %p\n",&printfX);
    void (*pf)() = printfX;
    printf("调用pf()\n");
    pf();

    printf("调用(*pf)()\n");
    (*pf)();
}

这里写图片描述



定义函数指针的别名

typedef void (*PFUNC)(); 
PFUNC pf = printfX;


函数指针数组
声明

void (*functionArray[N])(void)

实例

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void  printfX(){
    printf("xxxxxxxx\n");
    printf("xxxxxxxx\n");
    printf("xxxxxxxx\n");
    printf("xxxxxxxx\n");
}

void  printfO(){
    printf("oooooooo\n");
    printf("oooooooo\n");
    printf("oooooooo\n");
    printf("oooooooo\n");
}

void  printfA(){
    printf("AAAAAAAAA\n");
    printf("AAAAAAAAA\n");
    printf("AAAAAAAAA\n");
    printf("AAAAAAAAA\n");
}

typedef void (*PF)(); 
int main(void){

    void (*pfarr[3])() = {printfX,printfO,printfA};
    int choice;
    scanf("%d",&choice);
    while(choice >= 0 && choice <3){
        pfarr[choice]();//一定要记得写括号,不然只 表示函数的引用而不是调用函数 
        scanf("%d",&choice) ;
    }
}

这里写图片描述



函数指针的应用——函数回调

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define arrLen 10
void selectSort(int *p,int n,int (*pf)(int ,int )){

    for(int i = 0;i<n-1;i++){
        for(int j=i+1;j<n;j++){

            if(pf(p[i],p[j])){
                p[i] = p[i]^p[j];
                p[j] = p[i]^p[j];
                p[i] = p[i]^p[j]; 
            }
        }
    }
}
//只需修改啊a>B 或a<b,不用修改 selectSort就可以实现降序升序排列
//这就是函数回调带来的便利
//这也是函数指针一种应用 
int callBack(int a,int b){

    return a<b?1:0;
}
int main(void){
    srand(time(NULL));
    int arr[arrLen] ;
    for(int i = 0;i<arrLen;i++){
        arr[i] = rand()%100+1; 
    }

    selectSort(arr,arrLen,callBack);
    for(int i = 0;i<arrLen;i++){
        printf("%5d",arr[i]);
    }

    return 0;
}

这里写图片描述



结构体指针的一级和二级比较

//结构体排序 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
    char name[30];
    float score;
}STU;

int cmp(const void *a,const void *b){

    if(strcmp((*(STU *)a).name,(*(STU *)b).name)>0){ //一级比较 
        return 1;
    }
    else if(strcmp((*(STU *)a).name,(*(STU *)b).name) == 0){ //二级比较 
        if((*(STU *)a).score - (*(STU *)b).score >0){
            return 1;
        } else
            return -1;
    }
}

int main(void) {

    STU stu[] = {{"xxx",19},{"abc",65},{"xxx",45},{"abd",90}};
    int len  = sizeof(stu)/sizeof(STU);
    qsort(stu,len,sizeof(STU),cmp);

    for(int i = 0;i<len;i++){

        printf("%5s %5f\n",stu[i].name,stu[i].score);
    }
    return 0;   
} 

这里写图片描述



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值