George
George

Reputation: 7317

Returning a call to a pointer to a function in C

From the book 21st Century C:

Conceptually, the syntax for a function type is really a pointer to a function of a given type. If you have a function with a header like:

double a_fn(int, in); //a declaration

then just add a star (and parens to resolve precedence) to describe a pointer to this type of function:

 double (*a_fn_type)(int, int); //a type: pointer-to-function

Then put typedef in front of that to define a type:

 typedef double (*a_fn_type)(int, int); //a typedef for a pointer to function

Now you can use it as a type like any other, such as to declare a function that takes another function as input:

double apply_a_fn(a_fn_type f, int first_in, int second_in){
  return f(first_in, second_in); //shouldn't this be *f(first_in, second_in) ?
}

Question: Shouldn't the return value of this very last function be *f(first_in, second_in), since f is a pointer to a function and *f denotes the actual function?

Upvotes: 2

Views: 47

Answers (1)

Robert Columbia
Robert Columbia

Reputation: 6408

The dereferencing operator for calling a function pointer is optional in C.

Upvotes: 1

Related Questions