Skip to content

Instantly share code, notes, and snippets.

@Mithrandir0x
Created November 16, 2012 12:14
Show Gist options
  • Save Mithrandir0x/4086831 to your computer and use it in GitHub Desktop.
Save Mithrandir0x/4086831 to your computer and use it in GitHub Desktop.
Example of function pointers in C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.141592653589793
double f_a(double x) { return x*x + sin(x) - PI; }
typedef double (*MATH_FUNCTION)(double);
double calculate_f_0(double (*f)(double))
{
return (*f)(0);
}
int main()
{
MATH_FUNCTION F = &f_a;
printf("%.15e\n", calculate_f_0(&f_a));
printf("%.15e\n", calculate_f_0(F));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment