Tuesday, July 05, 2011

C Function pointers

Just some old snippet on the C syntax for function pointers.
#include <stdio.h>

typedef int (*TFunction)();

TFunction gVarFunction;

int mf1(int x) { printf("hallo %d\n", x); return 0; }
int mf2(int x) { printf("hello %d\n", x); return 0; }

void StoreFunction(TFunction param) { gVarFunction = param; }

int main (int argc, const char * argv[]) {
  StoreFunction(mf1);
  gVarFunction(2);
  
  StoreFunction(mf2);
  gVarFunction(6);

  return 0;
}
The output being:
> hallo 2
> hello 6

No comments: