Why are both of these function pointers legal in C / C ++?

I have two test functions:

int apply_a(int (*fun)(int, int), int m, int n) { return (*fun)(m,n); } int apply_b(int (*fun)(int, int), int m, int n) { return fun(m,n); } 

they seem to return something else, so why do they both give the same result?

 int add(int a, int b) {return a + b;} int res_a = apply_a(add, 2, 3); // returns 5 int res_b = apply_b(add, 2, 3); // returns 5 

I would suggest that one of them returns the address of the pointer or the pointer itself; not the value stored on the pointer ...

So why is he doing this?

+8
c ++ c pointers return function-pointers
source share
4 answers

Because C ++ offers syntactic sugar when it comes to accessing non-member functions and using pointers to them.

You can get the address of such a function:

 int someFunc(int); 

through:

 int (* someFuncPtr)(int) = someFunc; 

or

 int (* someFuncPtr)(int) = &someFunc; 

There is also syntactic sugar for using such a pointer, or calling the point-to function with:

 (*someFuncPtr)(5); 

or with simplified syntax:

 someFuncPtr(5); 
+7
source share

(*fun)(m,n) same as fun(m,n) because of the rules in C and C ++ that convert functions to function pointers.

In C 2011, a rule is clause 6.3.2.1 4: "A function designator is an expression that has a function type. Unless it is an operand of the sizeof operator, the _Llignof operator, or a unary operator & , the designation of a function with the type of the returned function of type is converted to an expression , which is of type β€œpointer to the return type of the function.” In C ++, the rule is clause 4.3.

Note that a function designation is not just an identifier that calls a function. It can be an identifier, or it can be another expression. For example, if foo is the name of a function, it is automatically converted to a pointer to the function above. Then, since foo is a pointer to a function, *foo is a function. This means that you can write:

 (*fun)(m,n) 

As a result, fun automatically converted to a pointer, then * evaluates the function, then *fun converted back to a pointer, then the function is called. You can continue this and write:

 (**************fun)(m,n) 

This is the same as fun(m,n) . Each * executes the function again, but the compiler automatically converts it back to a pointer. You can continue this battle forever, but the compiler will always win.

In fact, they all have the same effect:

 (&fun)(m,n) ( fun)(m,n) (*fun)(m,n) 
+6
source share

This is because you are not returning the memory addresses of these values. Calling a function with a pointer does not change its value, which still calls the function. What you can do is return the value of the variable and then get its memory address, for example:

 int result = fun(m,n); cout << "the result " << result << " pointing at " << &result << endl; 
+1
source share

In C and C ++, the function name is also a pointer to the function code. Like any pointer, you can dereference them with * , which in the case of function pointers means calling a function, when in addition to dereferencing, you also use paranthesis after them, as in your case apply_a . But also a valid call to the C and C ++ functions calls them simply by their name, which is the apply_b event.

0
source share

All Articles