-3

I don't know why there is int in parenthesis next to the *Predicate

typedef bool (*Predicate)(int);
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
jeanrwx
  • 9
  • 1
  • 1
    `Predicate` is a function pointer; see https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work for what they do and how they work. – zneak Jan 18 '19 at 00:15

3 Answers3

4

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;
Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
  • 1
    I believe the [generally] preferred syntax is: `Predicate p = always_true;` The `&` is unnecessary. This makes it syntax compatible with `Predicate q = p;`. – Craig Estey Jan 18 '19 at 00:24
4

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.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

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");
}
0___________
  • 60,014
  • 4
  • 34
  • 74