Created
February 27, 2013 00:43
-
-
Save thebyrd/5043815 to your computer and use it in GitHub Desktop.
Closures in C have a nasty syntax. you can short circuit typedef to create a fake type (in this case iTOi) that can be used as a parameter type.
This file contains 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
typedef int (*iTOi) (int a); | |
int incr (int n) { return n+1; } | |
int decr (int n) { return n-1; } | |
void start(iTOi cc) { | |
printf("%d\n", cc(0)); | |
} | |
int main ( int argc, char *argv[] ) { | |
start(&incr); // returns 1 | |
start(&decr); // returns -1 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment