对函数指针的理解学习

    技术2022-05-19  21

    #include <stdio.h> int f1(int x,int y) {     return x+y; } int f2(int a,int b) {    return a-b;   } //将一个返回值为int,参数为两个int类型的函数指针做为形式参数 int caller(int(*ptr)(int x,int y),int a,int b) {     return (*ptr)(a,b);     //或者return ptr(a,b); } int main() {    int i = 15;    int j = 13;     //申明虑数指针(可以指向指向返回值为int,并有两个int参数的函数)     int (*p) (int a,int b);     //指函数指针指向f1     p= f1;     //开始调用     //直接通过函数指针调用所指向的函数     int x = p(i,j);     //指对将函数指针做为参数传递给第三个调用方来调用     int y = caller(p,i,j);          printf("p(%d,%d):%d/n",i,j,x);     printf("caller(p,%d,%d):%d/n",i,j,y);    //指函数指针指向f2     p= f2;     //开始调用     x = p(i,j);     y = caller(p,i,j);          printf("p(%d,%d):%d/n",i,j,x);     printf("caller(p,%d,%d):%d/n",i,j,y); } 返回值:    p(15,13):28    caller(p,15,13):28    p(15,13):2    caller(p,15,13):2

     

     

     


    最新回复(0)