Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Just need help understanding the syntax of passing a function pointer as an argument
What does the very first double in front of the *f mean?
C++
double integrate(double(*f)(double) , double a , double b);

If i for example call something like:
result = integrate(sin, 0.0, PI/2);

I know sin's produced pointer is being passed as an argument to integrate, and 0.0 gets assigned to 'a', and PI/2 gets assigned to 'b'. Of course this is just a scenario, where PI would be declared already. I am reading this off a book and the author never explains what the first double after *f means, even if it is an argument, is that the argument that takes the pointer from sin? If it is why is it not *double instead? I need an explanation for what the first double after *f does. Thank You.

Also, if there is a better way to write this, please let me know so i can put it in my notes.
Posted
Updated 8-Apr-15 14:48pm
v2
Comments
barneyman 8-Apr-15 22:35pm    
the [double(*f)(double)] is mandating a function signature that takes a double and returns a double i.e. double sin(double rad), &sin would qualify

Let's break double(*f)(double) apart;

The asterisk inside parenthesis here indicates that it's a function pointer (*f), the f is the name we give it, just like a is the name of the second parameter in your example.

The type (or types if it's pointing to a method with more than one argument) in parenthesis after the (*f) is the argument signature of the method, in this case, (double), means it's pointing to a method that takes a single argument of type double.

The leading type that appears before the (*f) is the return type of the function pointed to, in this case double.

So by putting all of the above together you can read that f is a pointer to a function taking a single double as argument and returning a double. And sin is such a method.

The function pointer needs to be specific enough to detail both the arguments passed, and the return value of the function pointed to.

This is pretty much the standard way of expression function pointers in C.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
CPallini 9-Apr-15 3:13am    
5.
kujenjishi 9-Apr-15 16:23pm    
Yep, this is what i was looking for, thanks.
Just to add something to solution 1, when dealing with function pointers, usually a typedef is handy. See, for instance: "Learn C the hard way: Exercise 18: Pointers To Functions"[^].
 
Share this answer
 
One point is missing to clarify why a function pointer is always in round brackets:
The brackets drive the compiler to take '*f' as a function pointer not a pointer to the result type.
I.e:
C++
void *f(int a);      //This is a prototype for a function f returning a pointer to void
int  *f(int a);      //This is a prototype for a function f returning a pointer to int
....                 //and so on...
double (*f)(int a);  //This is a pointer to function, because the asterisk is to be
                     //referred to function name, as a pointer to function,
                     //and nothing else!
int  (*af[])(int a); //What's this?!? :D
                     //It's an array of function pointers taking an int as parameter
                     //and returning an int.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900