Last active
December 15, 2015 12:19
-
-
Save wolph/5259600 to your computer and use it in GitHub Desktop.
Passing functions in C with function return type casting
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
#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