I don't know why there is int
in parenthesis next to the *Predicate
typedef bool (*Predicate)(int);
I don't know why there is int
in parenthesis next to the *Predicate
typedef bool (*Predicate)(int);
This declares Predicate
as the type of pointers to functions that take an int
parameter and return a bool
result. int
is in parentheses to indicate that it is a parameter type.
You might use it like this:
typedef bool (*Predicate)(int);
bool always_true(int) { return true; }
Predicate p = &always_true;
typedef bool (*Predicate)(int);
means you're declaring a type named Predicate
that is a function pointer for a function that accepts a single int
argument, and returns bool
. A function that meets the requirements would be:
bool is_zero(int val) {
return val == 0;
}
and you could declare a variable like:
Predicate mypredicate = is_zero;
In this case, the likely intent is to declare a function that performs a filtering operation using a runtime supplied predicate function; it would accept Predicate
as one of its arguments.
It ia a typedef od the function pointer having one parameter of type int and returning bool
function pointers are the only place it is good to typedef pointers.
typedef bool (*Predicate)(int);
bool is_zero(int x)
{
return x ==0;
}
Predicate myfuncptr = is_zero;
int main()
{
if(myfuncptr(5))
printf("Zero");
else
printf("Not zero");
}