Function pointer as an argument

inquisitive picture inquisitive · Nov 24, 2009 · Viewed 93.2k times · Source

Is it possible to pass a function pointer as an argument to a function in C?

If so, how would I declare and define a function which takes a function pointer as an argument?

Answer

Mehrdad Afshari picture Mehrdad Afshari · Nov 24, 2009

Definitely.

void f(void (*a)()) {
    a();
}

void test() {
    printf("hello world\n");
}

int main() {
     f(&test);
     return 0;
}