• 函数指针与回调函数


    #include <stdio.h>
    
    
    void function(void){
    }
    int function1(void){
        return 1;
    }
    int function2(int a){
        return a + 1;
    }
    void function3(int s){
    }
    
    int myMax(int a, int b){ if (a > b)return a; return b; }
    int myAdd(int a, int b){ return a + b; }
    int myMinus(int a, int b){ return a - b; }
    
    /** 
        这个是本段程序的最精华部分!!!还得再默写一遍!!!
    */
    int myFunction(int(*p)(int, int), int a, int b){
        return p(a, b);
    }
    
    int main(){
    
        void(*p)(void) = function;//这个声明跟下面这里必须严格一一对应
        int(*p1)(void) = function1; 
        int(*p2)(int) = function2;
        void(*p3)(int) = function3;
        printf("函数function的地址是:%p
    ", p);
        printf("函数function的地址是:%p
    ", function);//函数名跟数组名一样,都是用来代表当前这段内容的地址
        printf("函数function1的地址是:%p
    ", p1);
        printf("函数function2的地址是:%p
    ", p2);
        printf("函数function3的地址是:%p
    ", p3);
        int result1 = p2(3);
        int result2 = function2(3);
        //那返回的结果当然也一样喽
    
        int r1 = myFunction(myMax,3, 4);
        int r2 = myFunction(myAdd,3, 4);
        int r3 = myFunction(myMinus,3, 4);
        printf("结果分别是:%d %d %d", r1, r2, r3);
    
    
    
        getchar();
    
    
    }
  • 相关阅读:
    高斯金字塔、拉普拉斯金字塔
    边缘检测:Canny算子,Sobel算子,Laplace算子
    数据结构-排序
    数据结构-查找
    数据结构-图
    数据结构-树
    数据结构-串
    数据结构-栈和队列
    数据结构-链表
    数据结构-线性表存储
  • 原文地址:https://www.cnblogs.com/letben/p/5225521.html
Copyright © 2020-2023  润新知