简单回调函数例子

#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
",(const char*)str);
}

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

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

原文地址:https://www.cnblogs.com/muahao/p/7225127.html