Cコールバック関数

test.cの、TEST.H、main.cの3つのファイルを準備する1

2.ファイルそうコンパイル

3.メインコンパイル

によってコールバック関数ポインタ

TEST.H

書式#include <stdio.hに>

say_hello();

INT caculate(int型のx、int型 Y、INT(* cacauFunc)(int型のx、int型 Y))。

test.cの

#include " TEST.H "

 say_hello(文字 * 名){
    printf(" こんにちは%sの\ nは" 、名);
}

INT caculate(int型のx、int型 Y、INT(* cacauFunc)(int型のx、int型Y)){
    printf(" X =%dを受け取り、yは%D \ N = " 、x、y)は、
    リターンcacauFunc(X、Y)
}

main.cの

#include " TEST.H "

INT追加(int型のx、int型Y){
     リターン X + Y。
}

INTサブ(int型のx、int型Y){
     リターン X - Y。
}

ボイドtestSo(){
    say_hello(" guanxianseng " );
}

ボイドtest_call_func_pointer(){
    printf(" スタート\ N " );
    INT X = 5、Y = 1、結果= 0 
    結果 = caculate(X、Y、ADD)。
    printf(" 結果=%D \ n " 、結果)。
    結果 = caculate(X、Y、サブ)。
    printf(" 結果=%D \ n " 、結果)。
}

INT メイン(){
    test_call_func_pointer();

    リターン 0 ;
}

コンパイルtest.so

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

コンパイルメイン

gccの main.cの-L。メイン-o -ltest

テスト実行

 

おすすめ

転載: www.cnblogs.com/luckygxf/p/11894870.html