Created
December 26, 2013 14:29
-
-
Save nkoneko/8134456 to your computer and use it in GitHub Desktop.
Function Pointer.
Not a good example.
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> | |
#define USE_BINARY_FUNC typedef struct { \ | |
T (*fn)(T, T); \ | |
} T##_binfn; | |
USE_BINARY_FUNC(int) | |
USE_BINARY_FUNC(float) | |
int add(int x, int y) { return x + y; } | |
int sub(int x, int y) { return x - y; } | |
float mult(float x, float y) { return x * y; } | |
int main(void) | |
{ | |
int_binfn int_func; | |
float_binfn float_func; | |
int_func.fn = add; | |
printf("%d\n", int_func.fn(3, 4)); | |
int_func.fn = sub; | |
printf("%d\n", int_func.fn(3, 4)); | |
float_func.fn = mult; | |
printf("%f\n", float_func.fn(1.1, 1.2)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment