简单使用--函数指针和回调函数

#include <unistd.h>
#include<stdio.h>

//要求在run函数里面每隔一秒运行一个函数,这个函数由main.c指定(函数放在main.c里面)

void (*__handle)(void) = NULL; //定义函数指针


void run(){
    int cn = 0;

    while (1)
    {
        if( __handle ){
            __handle();
        }else{
            printf( "step NOT running... \n") ;
        }
        cn++;
        sleep(2);
    }  
}

#include<stdio.h>
#include "run.c"

void (*__handle)(void) ;

void step_cb(void){
    printf( "step_cb running... \n") ;
}


int main(){

    printf( "hello... \n") ;

    __handle = step_cb;
    // step = NULL;
    run();

    return 0;
}

输出:

posted @ 2021-06-13 14:08  方罗良  阅读(49)  评论(0)    收藏  举报