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

    1. 准备三个文件test.c, test.h, main.c

    2. 编译生成so文件

    3. 编译生成main

    通过函数指针回调函数

    test.h

    #include <stdio.h>
    
    void say_hello();
    
    int caculate(int x, int y, int (*cacauFunc)(int x, int y));

    test.c

    #include "test.h"
    
    void say_hello(char *name){
        printf("hello %s
    ", name);
    }
    
    int caculate(int x, int y, int (*cacauFunc)(int x, int y)) {
        printf("receive x = %d, y = %d
    ", x, y);
        return cacauFunc(x, y);
    }

    main.c

    #include "test.h"
    
    int add (int x, int y) {
        return x + y;
    }
    
    int sub (int x, int y) {
        return x - y;
    }
    
    void testSo() {
        say_hello("guanxianseng");
    }
    
    void test_call_func_pointer(){
        printf("start
    ");
        int x = 5, y = 1, result = 0;
        result = caculate(x, y, add);
        printf("result = %d
    ", result);
        result = caculate(x, y, sub);
        printf("result = %d
    ", result);
    }
    
    int main(){
        test_call_func_pointer();
    
        return 0;
    }

    编译test.so

    gcc test.c -fPIC -shared -o libtest.so

    编译main

    gcc main.c  -L. -ltest -o main

    执行测试

  • 相关阅读:
    C++命名法则
    腾讯附加题---递归
    决策树
    ubuntu16.04安装后干的事
    node
    iview datetime日期时间限制
    GitLab CI/CD
    本地项目上传到github
    npm--配置私服
    gitlab添加yml文件.gitlab-ci.yml
  • 原文地址:https://www.cnblogs.com/luckygxf/p/11894870.html
Copyright © 2011-2022 走看看