简单回调函数例子

#cat para_callback.h
#ifndef PARA_CALLBACK_H
#define PARA_CALLBACK_H

typedef void (*callback_t) (void *);

extern void repeat_three_times (callback_t,void *);
#cat para_callback.c
#include "para_callback.h"

void repeat_three_times(callback_t f, void *para){
	f(para);
	f(para);
	f(para);
}

#cat main.c
#include <stdio.h>
#include "para_callback.h"

void say_hello(void *str){
	printf("Hello %s\n",(const char*)str);
}

void count_numbers(void *num){
	int i;
	for(i=1; i< (int)num; i++){
		printf("%d ", i);
		putchar('\n');
	}
}

int main(void){
	repeat_three_times(say_hello,"muahao");
	repeat_three_times(count_numbers,(void*)4);
	return 0;
}

posted @ 2017-07-23 16:23  苏小北1024  阅读(355)  评论(0编辑  收藏  举报