Created
January 9, 2020 03:37
-
-
Save frankie-yanfeng/988900e91b778c05a1d019d2a22ea935 to your computer and use it in GitHub Desktop.
C Callback function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* para_callback.c */ | |
#include "para_callback.h" | |
void repeat_three_times(callback_t f, void *para) | |
{ | |
f(para); | |
f(para); | |
f(para); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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