typedef int (*pf) needs explaining typedef int (*pf) needs explaining c c

typedef int (*pf) needs explaining


typedef int (*pf) (int, int);

This means that variables declared with the pf type are pointers to a function which takes two int parameters and returns an int.

In other words, you can do something like this:

#include <stdio.h>typedef int (*pf)(int,int);int addUp (int a, int b) { return a + b; }int main(void) {    pf xyzzy = addUp;    printf ("%d\n", xyzzy (19, 23));    return 0;}


typedef long int li;

assigns alternate name li to type long int.

In exactly the same way

typedef int (*pf) (int, int);

assigns alternate name pf to type int (*) (int, int). That all there is to it.

As you probably noticed, typedef declarations follow the same syntax as, say, variable declarations. The only difference is that the new variable name is replaced by the new type name. So, in accordance with C declaration syntax, the declared name might appear "in the middle" of the declarator, when array or function types are involved.

For another example

typedef int A[10];

declares A as alternate name for type int [10]. In this example the new name also appears "in the middle" of the declaration.


It's a function pointer prototype. You can then declare a function as an argument something like this:

void RegisterCallback(pf your_callback_func);

Then you can can call the function passed as a func ptr:

...your_callback_func(i, j);...