Skip to content

Instantly share code, notes, and snippets.

@nkoneko nkoneko/fnptr.c

Created Dec 26, 2013
Embed
What would you like to do?
Function Pointer. Not a good example.
#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
You can’t perform that action at this time.