Skip to content

Instantly share code, notes, and snippets.

@nkoneko
Created December 26, 2013 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkoneko/8134456 to your computer and use it in GitHub Desktop.
Save nkoneko/8134456 to your computer and use it in GitHub Desktop.
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