Reputation: 642
Generally, we use typedef
to get alternate names for datatypes.
For example --
typedef long int li; // li can be used now in place of long int
But, what does the below typedef do?
typedef int (*pf) (int, int);
Upvotes: 14
Views: 8603
Reputation: 320679
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.
Upvotes: 6
Reputation: 122483
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;
}
Upvotes: 28
Reputation: 7733
typedef
works as:
Define unknown type
with known types
.
So it defines function type that takes two int argument and return int.
Upvotes: 0
Reputation: 60017
The typedef
has the name pf
and it is for a function pointer that takes two integers as arguments and returns an integer.
Upvotes: 0
Reputation: 3462
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);
...
Upvotes: 0