Skip to content

Instantly share code, notes, and snippets.

@wolph
Last active December 15, 2015 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolph/5259600 to your computer and use it in GitHub Desktop.
Save wolph/5259600 to your computer and use it in GitHub Desktop.
Passing functions in C with function return type casting
#include <stdio.h>
int a(){
return 42;
}
double b(){
return 3.141592653589793;
}
char c(){
return '*';
}
int main(int argc, char **argv){
void* (*someFunc)();
someFunc = (void*(*)())&a;
printf("The result of a: %d\n", (int)((int(*)())someFunc)());
someFunc = (void*(*)())&b;
printf("The result of b: %f\n", (double)((double(*)())someFunc)());
someFunc = (void*(*)())&c;
printf("The result of c: %c\n", (char)((char(*)())someFunc)());
printf("The result of c: %d\n", (int)((int(*)())someFunc)());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment