zoukankan      html  css  js  c++  java
  • 函数指针与回调函数

    #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();
    
    
    }
  • 相关阅读:
    可执行程序的装载
    stdafx.h的作用
    AI调色板
    3ds max输出图片
    3ds max移除几何体的线段
    3ds max删除了对象后,还是将原来所有对象输出的原因
    vs win32 & MFC 指针默认位置
    3ds max 分离对象
    PDF
    endnote设置文献第二行悬挂缩进办法
  • 原文地址:https://www.cnblogs.com/letben/p/5225521.html
Copyright © 2011-2022 走看看