#include double foo(int a, float b) { return a+b; } int main() { double (*p)(int,float); p = foo; //p = &foo; // causes the same effect printf("%g\n",p(1,2)); printf("%g\n",(*p)(1,2)); // same thing return 0; } --------------------------------------------------------- #include void printBoard(double (*f)(double,double)) { int i,j; for(i=1; i<=10; ++i) { for(j=1; j<=10; ++j) printf("%.2f\t",f(i,j)); printf("\n"); } } double mul(double a, double b) { return a*b; } double div(double a, double b) { return a/b; } int main() { printBoard(mul); printf("\n\n"); printBoard(div); return 0; } --------------------------------------------------------- #include #include double integral(double (*f)(double), double x1, double x2) { static const double eps = 1e-7; double x,sum = 0; for(x=x1; x<=x2; x+=eps) sum += eps*f(x); return sum; } double linear(double x) { return 2*x+1; // integral = x^2+x } double quadratic(double x) { return pow(x,2); // integral: x^3 / 3 } double exponential(double x) { return pow(M_E,x); // integral: e^x } int main() { printf("%g\n",integral(linear,0,2)); // 2^2+2 - 0 = 6 printf("%g\n",integral(quadratic,0,2)); // 2^3/3 - 0 = 8/3 = 2.6666 printf("%g\n",integral(cos,0,M_PI)); // sin(pi)-sin(0) = 0 printf("%g\n",integral(exponential,0,1)); // e-1 return 0; } --------------------------------------------------------- #include #include typedef double(*func_t)(double); // that is the strange correct syntax. // typedef double(*)(double) func_t; // this somewhat more reasonable syntax won't compile func_t greaterOnZero( func_t f, func_t g) { if(f(0) > g(0)) return f; return g; } int main() { printf("%f\n",greaterOnZero(sin,cos)(M_PI));// output: -1 return 0; } --------------------------------------------------------- #include typedef void (*fun_t)(int*,int*); void doNothing(int *x, int *y) { addstr("this key is not bound to a function"); } void left(int *x, int *y) {(*x)--;} void right(int *x, int *y) {(*x)++;} void up(int *x, int *y) {(*y)--;} void down(int *x, int *y) {(*y)++;} int main() { int x = 10, y =10,i; unsigned char c; initscr(); fun_t arr[256]; for(i=0; i<256; ++i) arr[i] = doNothing; arr['b'] = right; arr['v'] = left; arr['n'] = up; arr['m'] = down; while(1) { move(y,x); refresh(); c = getchar(); if(c == (unsigned char)27) break; arr[c](&x,&y); } endwin(); return 0; } --------------------------------------------------------- #include const int SIZE = 10; int less(double v1, double v2) { return v1 < v2; } int more(double v1, double v2) { return v1 > v2; } void swap(int *p1, int *p2) { int t = *p1; *p1 = *p2; *p2 = t; } void print(int *v) { int i; for(i=0; i #include #include typedef struct shape Shape; typedef void (*FunV)(Shape*); typedef double (*FunD)(Shape*); typedef void (*FunSB) (Shape*, char*); struct shape { double m_x,m_y; void *m_param; FunD area; FunV print; void (*move)(Shape*, double , double); }; typedef struct { double m_rad; } CircleParam; typedef struct { double m_width, m_height; } RectangleParam; //Shape void moveShape(Shape *shape, double dx, double dy) { shape->m_x += dx; shape->m_y += dy; } Shape* createShape(double x, double y, void *param, FunD area, FunV print) { Shape *p = (Shape*)malloc(sizeof(Shape)); p->m_x = x; p->m_y = y; p->m_param = param; p->area = area; p->print = print; p->move = moveShape; return p; } void freeShape(Shape* shape) { free(shape->m_param); free(shape); } // Circle double circleArea(Shape *shape) { double r = ((CircleParam*)(shape->m_param))->m_rad; return M_PI*r*r; } void circlePrint(Shape *shape) { double r = ((CircleParam*)(shape->m_param))->m_rad; printf("\n",shape->m_x,shape->m_y,r); } Shape * createCircle(double x, double y, double rad) { CircleParam *cp = (CircleParam*)malloc(sizeof(CircleParam)); cp->m_rad = rad; return createShape(x,y,cp,circleArea,circlePrint); } // Rectangle double rectangleArea(Shape *shape) { RectangleParam *rp = (RectangleParam*)shape->m_param; return rp->m_width * rp->m_height; } void rectanglePrint(Shape *shape) { RectangleParam *rp = (RectangleParam*)shape->m_param; printf("\n", shape->m_x,shape->m_y,rp->m_width,rp->m_height); } Shape *createRectangle(double x, double y, double width, double height) { RectangleParam *rp = (RectangleParam*)malloc(sizeof(RectangleParam)); rp->m_width = width; rp->m_height = height; return createShape(x,y,rp,rectangleArea,rectanglePrint); } int main() { const unsigned N = 7; unsigned i; Shape** shapes = (Shape**)malloc(sizeof(Shape*)*N); shapes[0] = createCircle(2,2,5); // a circle at (2,2) with radius = 5 shapes[1] = createRectangle(10,8,7,3.4);// a rectangle at (10,8) // width = 7 height = 3.4 shapes[2] = createRectangle(1,2,8,10); for(i = 3; iprint(shapes[i]); printf("area: %g\n",shapes[i]->area(shapes[i])); } for(i = 0; imove(shapes[i],1,2); printf("\nafter moving...\n"); for(i = 0; iprint(shapes[i]); printf("area: %g\n",shapes[i]->area(shapes[i])); } // free the allocated memory for(i = 0; i