Skip to content

Instantly share code, notes, and snippets.

@frankie-yanfeng
Created January 9, 2020 03:37
Show Gist options
  • Save frankie-yanfeng/988900e91b778c05a1d019d2a22ea935 to your computer and use it in GitHub Desktop.
Save frankie-yanfeng/988900e91b778c05a1d019d2a22ea935 to your computer and use it in GitHub Desktop.
C Callback function
/* 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, (void *)"Guys");
repeat_three_times(count_numbers, (void *)4);
return 0;
}
/* para_callback.c */
#include "para_callback.h"
void repeat_three_times(callback_t f, void *para)
{
f(para);
f(para);
f(para);
}
#ifndef CALLBACK_PARA_CALLBACK_H
#define CALLBACK_PARA_CALLBACK_H
typedef void (*callback_t)(void *);
extern void repeat_three_times(callback_t, void *);
#endif //CALLBACK_PARA_CALLBACK_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment