Pass function into a function as parameter by function pointer - C Pointer

C examples for Pointer:Function Pointer

Description

Pass function into a function as parameter by function pointer

Demo Code

#include <stdio.h>

void makeTable(int, int, double (*fp) (int));
double reciprocal(int);
double square(int);
    /*from   w  w  w.j  av  a2s.c o m*/
int main() {
    makeTable(1, 10, reciprocal);
}

void makeTable(int first, int last, double (*fp) (int)) {
    for (int i = first; i <= last; i++)
        printf("%2d   %0.3f\n", i, (*fp)(i));
}

double reciprocal(int x) {
    return 1.0 / x;
}

double square(int x) {
    return x * x;
}

Related Tutorials