Skip to content

Instantly share code, notes, and snippets.

@thebyrd
Created February 27, 2013 00:43
Show Gist options
  • Save thebyrd/5043815 to your computer and use it in GitHub Desktop.
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.
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