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

I have these 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 appear to return something different, so why do both of them yield 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've assumed that one of them would return the pointer address or the pointer itself; rather than the value stored on the pointer...
So why is it doing this?

Because C++ offers syntactic sugar when it comes to handling the address of non-member functions and using pointers to them.
One can get address of such function:
int someFunc(int);
with either:
int (* someFuncPtr)(int) = someFunc;
or:
int (* someFuncPtr)(int) = &someFunc;
There is also syntactic sugar for using such pointer, either call pointed-to function with:
(*someFuncPtr)(5);
or with simplified syntax:
someFuncPtr(5);

(*fun)(m,n) is the same as fun(m,n) due to rules in C and C++ that convert functions to pointers to functions.
In C 2011, the rule is clause 6.3.2.1 4: “A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, a function designator with type “function returning type” is converted to an expression that has type “pointer to function returning type”. In C++, the rule is clause 4.3.
Note that a function designator is not merely an identifier that names a function. It could be an identifier, or it could be another expression. For example, if foo is the name of a function, it is automatically converted to a pointer to a function by the above. Then, since foo is a pointer to the function, *foo is the function. This means you can write:
(*fun)(m,n)
The result is that fun is automatically converted to a pointer, then * evaluates to the function, then *fun is 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 * produces 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, these all have the same effect:
(&fun)(m,n)
( fun)(m,n)
(*fun)(m,n)

It is because you are not returning memory addresses of these values.
Calling a function with pointer does not change it's value it's still calling the function. What you can do is return a value to a variable and then get it's memory address such as:
int result = fun(m,n);
cout << "the result " << result << " pointing at " << &result << endl;

In C and C++ name of functions are also the pointers to the function code. As any pointer you can dereference them using *, which in case of function pointers mean invocation of the function when in addition to dereferencing you use also paranthesis after them, like in your apply_a case. But also valid invocation of C and C++ function is calling them simply by their name, which is apply_b case.

Related

Do you need to use "&" for a function that has a function pointer as a parameter? [duplicate]

So I figured when making function pointers, you do not need the operator & to get the address of the initial function:
#include <stdio.h>
double foo (double x){
return x*x;
}
int main () {
double (*fun1)(double) = &foo;
double (*fun2)(double) = foo;
printf("%f\n",fun1(10));
printf("%f\n",fun2(10));
printf("fun1 = %p \t &foo = %p\n",fun1, &foo);
printf("fun2 = %p \t foo = %p\n",fun2, foo);
int a[10];
printf(" a = %p \n &a = %p \n",a,&a);
return 0;
}
output:
>./a.out
100.000000
100.000000
fun1 = 0x4004f4 &foo = 0x4004f4
fun2 = 0x4004f4 foo = 0x4004f4
a = 0x7fff26804470
&a = 0x7fff26804470
Then I realized this is also true for arrays, meaning that if you have int a[10] both a and &a point to the same location. Why is that with arrays and functions? Is the address saved in a memory location that has the same address as the value(address) being saved in it?
Given int a[10], both a and &a yield the same address, yes, but their types are different.
a is of type int[10]. When it is implicitly converted to a pointer type, the pointer is of type int* and points to the initial element of the array. &a is of type int (*)[10] (that is, a pointer to an array of ten integers). Because there can be no padding in an array, they both yield pointers with the same value, but the pointers have different types.
Functions are similar to arrays, but not entirely the same. Your function foo is of type double(double). Whenever foo is used in an expression and is not the operand of the unary & operator, it is implicitly converted to a pointer to itself, which is of type double(*)(double).
So, for all practical purposes, the name of a function and a pointer to the same function are interchangeable. There are some subtleties, all of which I discuss in an answer to "Why do all these crazy function pointer definitions all work? What is really going on?" (That question was asked about C++, but the rules for nonmember functions in C++ are the same as for functions in C.)
No, there's no extra storage dedicated to pointing to the function/array.
With most variables variable_name has a meaning other than getting the address of that variable, so you need to use &variable to get the address.
With a function or array, function_name (by itself, not followed by parentheses) doesn't have any other meaning, so there was no problem with interpreting it as taking the address of the function.
Likewise in reverse: a normal pointer needs to be dereferenced explicitly, but a pointer to a function doesn't (again, because there's no other reasonable interpretation), so given a pointer to a function like:
int (*func)(param_list);
The following are equivalent to each other -- both call whatever function func points at:
(*func)(params);
func(params);
fun and &fun are exactly the same (except that sizeof(f) is illegal).
a and &a are the same up to pointer arithmetic: a + 10 == &a + 1, because 10*sizeof(*a) == sizeof(a) (where sizeof(*a) == sizeof(int)).
Basically, since the function name is "known" to be a function, the & is not strictly necessary. This behavior is the same for arrays. Recall that a function itself is not a variable, so it behaves a little differently than you might expect sometimes. If you have the 2nd edition of K&R, you can check out section 5.11 on pointers to functions, or the reference manual at the end,
Section A7.1 Pointer generation: If the type of an expression or
subexpression is "array of T" for some type T, then the value of the
expression is a pointer to the first object in the array, and the type
of the expression is altered to "pointer to T." This conversion does
not take place of the expression is the operand of the unary &
operator, ... Similarly, an expression of type "function returning T,"
except when used as the operand of the & operator, is converted to
"pointer to function returning T."
Section A7.4.2 Address Operator: The unary & operator takes the address
of its operand.... The result is a pointer to the object or function
referred to by the lvalue. If the type of the operand is T, the type
of the result is "pointer to T."
As far as I know, this is the same for C99.
printf("fun1 = %p \t &foo = %p\n",fun1, foo);
Here your are calling foo by passing Function Pointer with pass by value
and
printf("fun2 = %p \t foo = %p\n",fun2, &foo)
Here you are calling &foo by passing function Pointer with pass by reference
in both case your are calling the printf with function pointer only.
Remember foo itself is function pointer value and `not a variable.
Same happens with array.
int arr[10] translates into get continuous block of 10 Integers and address of first element is stored into arr. so arr is also a pointer.

What does the value from dereferencing a function pointer means

#include <iostream>
void PrintTheValue(int(*func)(int a));
int main(int argc, char **argv) {
PrintTheValue([](int a) {return a; });
return 0;
}
void PrintTheValue(int(*func)(int a)) {
std::cout << *func << std::endl;
}
In my concept of understanding the func, it would be a pointer to an int passed by value. But in this case I'm passing a lambda which doesn't seem to be called anywhere. (So there isn't any value at all?)
When I run this, it doesn't break the program, but instead printed 00EE6F80.
What does this address mean? I have no idea how to interpret it.
In my concept of understanding the func, it would be a pointer to an int passed by value.
func is a pointer to function, which takes an int and returns int.
But in this case I'm passing a lambda which doesn't seem to be called anywhere.
You're passing a lambda without capturing, which could convert to pointer to function implicitly. In PrintTheValue, *func, i.e dereference on the pointer results in a reference to function, and for being passed to operator<< of std::cout, it converts to function pointer again, then converts to bool with value true (as a non-null pointer), then you should get the result 1 (or true with the usage of std::boolalpha). If you want to call on func you could func(42) (or (*func)(42)).
A lambda is an unnamed function object of the closure type.
The crucial part for this case, this class does not overload the operator<<.
When you dereference the passed lambda in *func, there is no overload for operator<<, so it converts to nearest acceptable result which is bool (at first it reverts to a regular pointer).
The documentation:
Dereferencing a function pointer yields the lvalue identifying the pointed-to function
int f();
int (*p)() = f; // pointer p is pointing to f
(*p)(); // function f invoked through the function lvalue
// But no sense in *p
It should print 1 (since non-null pointer), which it does for me (g++). The language does allow us to do so, but there's no sense in dereferencing a pointer to function without calling the function. All the peculiarities of the function pointers are due to that they have one reasonable usage, so anything you do with them will support that usage - #Pete Becker
For more on Function Pointers check here, it will aid.

Function Pointer ambiguity in C++

I have two questions :
Q1) Are the function names themselves pointers ??
If they are pointers , then what values are stored in them?
Else if they are not pointers ,then,
what are they and what values are stored in them?
If we consider that function names are pointers. Then :
void display(){...}
int main ()
{
void (*p)();
**p=display; //Works (justified**, because we are assigning one pointer into another)
**p=&display; //Works (Not justified** If function name is a pointer (let say type*) , then &display is of datatype : type**. Then how can we assign type** (i.e. &display) into type * (i.e. p)??)
**p=*display; //Works (Not justified** If function name is a pointer ( type *) ,then, how can we assign type (i.e. *display) into type * (i.e. p) ?? )
}
Again ,
cout<<display<<";"<<&display<<";"<<*display;
Prints something like :
0x1234;0x1234;0x1234
[1234 is just for example]
[OMG! How is this possible ??How can address of a pointer, address it is pointing to and the value at pointed address all be same?]
Q2) What value is stored in a user defined pointer to a function ?
Consider the example :
void display(){...}
int main()
{
void (*f)();
f=display;
f=*f; // Why does it work?? How can we assign type (i.e. *f ) into type * (i.e. f).
cout<<f<<";"<<&f<<";"<<*f;
//Prints something like :
0x1234;0x6789;0x1234
}
[First two outputs are justified... But how can the value in a pointer (address it is pointing to) be equal to the value stored in the pointed address? ]
Again :
f=*********f; // How can this work?
I searched it online but all info that is available is regarding usage and example code to create function pointers. None of them say about what they are or how they are different from normal pointers.
So I must be missing something very basic thing. Please point me out what I am missing. (Sorry for my ignorance being a beginner.)
Are the function names themselves pointers?
No. However, in some contexts, a function can be automatically converted to a pointer-to-function. The standard says in paragraph 4.3:
An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.
(A function name designates an lvalue, but there can be other lvalues of function type).
In your examples
p = display;
p = *p;
there's exactly this kind of automatic conversion. display and *p are lvalues of a function type, and when needed, they are silently and automatically converted to a pointer-to-function type.
p = *display;
Here the conversion occurs twice: first display is converted to a pointer for the * operator, then it is dereferenced, then converted to a pointer again for the = operator.
cout << display << ";" << &display << ";" << *display;
Here, display is converted to a pointer for operator <<; &display is already a pointer because & is a normal address-taking operator; and *display is converted to a pointer for operator << while inside it display is converted to a pointer for operator *.
f = *********f;
There are many conversions of this kind in this expression. Count them yourself!

Why does setting function pointer as pointer and pointerpointer work? [duplicate]

So I figured when making function pointers, you do not need the operator & to get the address of the initial function:
#include <stdio.h>
double foo (double x){
return x*x;
}
int main () {
double (*fun1)(double) = &foo;
double (*fun2)(double) = foo;
printf("%f\n",fun1(10));
printf("%f\n",fun2(10));
printf("fun1 = %p \t &foo = %p\n",fun1, &foo);
printf("fun2 = %p \t foo = %p\n",fun2, foo);
int a[10];
printf(" a = %p \n &a = %p \n",a,&a);
return 0;
}
output:
>./a.out
100.000000
100.000000
fun1 = 0x4004f4 &foo = 0x4004f4
fun2 = 0x4004f4 foo = 0x4004f4
a = 0x7fff26804470
&a = 0x7fff26804470
Then I realized this is also true for arrays, meaning that if you have int a[10] both a and &a point to the same location. Why is that with arrays and functions? Is the address saved in a memory location that has the same address as the value(address) being saved in it?
Given int a[10], both a and &a yield the same address, yes, but their types are different.
a is of type int[10]. When it is implicitly converted to a pointer type, the pointer is of type int* and points to the initial element of the array. &a is of type int (*)[10] (that is, a pointer to an array of ten integers). Because there can be no padding in an array, they both yield pointers with the same value, but the pointers have different types.
Functions are similar to arrays, but not entirely the same. Your function foo is of type double(double). Whenever foo is used in an expression and is not the operand of the unary & operator, it is implicitly converted to a pointer to itself, which is of type double(*)(double).
So, for all practical purposes, the name of a function and a pointer to the same function are interchangeable. There are some subtleties, all of which I discuss in an answer to "Why do all these crazy function pointer definitions all work? What is really going on?" (That question was asked about C++, but the rules for nonmember functions in C++ are the same as for functions in C.)
No, there's no extra storage dedicated to pointing to the function/array.
With most variables variable_name has a meaning other than getting the address of that variable, so you need to use &variable to get the address.
With a function or array, function_name (by itself, not followed by parentheses) doesn't have any other meaning, so there was no problem with interpreting it as taking the address of the function.
Likewise in reverse: a normal pointer needs to be dereferenced explicitly, but a pointer to a function doesn't (again, because there's no other reasonable interpretation), so given a pointer to a function like:
int (*func)(param_list);
The following are equivalent to each other -- both call whatever function func points at:
(*func)(params);
func(params);
fun and &fun are exactly the same (except that sizeof(f) is illegal).
a and &a are the same up to pointer arithmetic: a + 10 == &a + 1, because 10*sizeof(*a) == sizeof(a) (where sizeof(*a) == sizeof(int)).
Basically, since the function name is "known" to be a function, the & is not strictly necessary. This behavior is the same for arrays. Recall that a function itself is not a variable, so it behaves a little differently than you might expect sometimes. If you have the 2nd edition of K&R, you can check out section 5.11 on pointers to functions, or the reference manual at the end,
Section A7.1 Pointer generation: If the type of an expression or
subexpression is "array of T" for some type T, then the value of the
expression is a pointer to the first object in the array, and the type
of the expression is altered to "pointer to T." This conversion does
not take place of the expression is the operand of the unary &
operator, ... Similarly, an expression of type "function returning T,"
except when used as the operand of the & operator, is converted to
"pointer to function returning T."
Section A7.4.2 Address Operator: The unary & operator takes the address
of its operand.... The result is a pointer to the object or function
referred to by the lvalue. If the type of the operand is T, the type
of the result is "pointer to T."
As far as I know, this is the same for C99.
printf("fun1 = %p \t &foo = %p\n",fun1, foo);
Here your are calling foo by passing Function Pointer with pass by value
and
printf("fun2 = %p \t foo = %p\n",fun2, &foo)
Here you are calling &foo by passing function Pointer with pass by reference
in both case your are calling the printf with function pointer only.
Remember foo itself is function pointer value and `not a variable.
Same happens with array.
int arr[10] translates into get continuous block of 10 Integers and address of first element is stored into arr. so arr is also a pointer.

Pointers to Functions (C++)

I am studying a C++ tutorial. I can't understand this example on Pointers to Functions.
Here it is:-
// pointer to functions
#include <iostream>
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
The lines "m = operation (7, 5, addition);" and "n = operation (20, m, minus);" are treated the same way, but while minus has been declared as a pointer to function, addition hasn't. So, how did they both work the same way?
Using a function name as an argument parameter in a function call, or on the right-hand side of the assignment operator in C/C++, causes a conversion to a function pointer to the original function.
So for instance if you have a function like
void my_function(int a, int b);
If you use the identifier my_function on the right-hand side of the assignment operator like this:
void (*my_function_ptr)(int, int) = my_function;
Then my_function implicitly converts from a function object to a function pointer of type void (*)(int, int), initializing the identifier my_function_ptr so that it points to my_function. The same situation would also occur when passing my_function to another function like:
void another_function(int, void (*)(int, int));
another_function(5, my_function);
In the call to another_function(), the identifier my_function is again converted to a pointer to the original function.
Finally, keep in mind this only occurs if you simply pass the identifier name to a function argument, or put it on the right-hand side of the assignment operator. Adding a function call using the () symbols and an optional argument list (i.e., my_function(5, 6)) will evaluate the function, not cause a conversion to a function pointer.
The type of addition is int (&)(int,int) which can decay into a pointer of type int (*)(int,int) which is same as that of operation function's third parameter. So you can pass addition as third argument to the function operation.
The type of subtraction is also the same as that of addition. In your code, the address of subtraction is first stored as local variable of the compatible type, and then that variable is passed as argument to operation function.
In case of addition, it's address is not stored as local variable, instead its passed as such to operation. Its initializing the function's third parameter directly with the function's address, without using any local variable.
A conversion from int (&)(int,int) to int (*)(int,int) occurs in both cases. Its just that with substration, the conversion occurs when initializing the local variable, and with addition, the conversion occurs when initializing the function parameter.
An analogy would be this:
void f(double a, double b) {}
int main()
{
double x = 100;//first store 100 in a local variable
f(x, 100); //pass the local variable as first arg,
//and pass 100 as second arg without using any local variable.
}
Note the type of 100 is int, so it first converts to double type, which is then stored as local variable x, which in turn is passed to the function f as first argument. And the second argument 100 is passed directly to the function, so even now it first converts to double and then it initializes b (the second parameter of the function).
Again, a conversion from int to double occurs in both cases. Its just that first argument conversion occurs when initializing the local variable x, and second argument conversion occurs when initializing the second parameter of the function.
name of the function in C is resolved to its address. So this works:
int (*minus)(int,int) = subtraction;
Both solutions work: the third argument of "operation" must be a function pointer.
Please note that the ampersand is optional:
m = operation (7, 5, &addition);
also works.
By passing "addition" to operation() you're effectively assigning it to "functocall". It's exactly the same as assigning "subtraction" to "minus". They work the same way because they are the same.
The real question is, why don't you need an ampersand (&) to take the function's address?
p.s. Apart from the gratuitous use of iostream, namespace and cout, this is actually a C tutorial. Right?
C++ provides implicit function-to-pointer conversion (see 4.3 in C++ 2003 standard). In your example, it is used both for assignment of subtraction to minus and for conversion of addition to the parameter type accepted by operation. So essentially both calls are done the same way, just in one case you explicitly created an intermediate variable of type pointer-to-function. And in both cases an optimizing compiler will simply pass the address of a corresponding function into operation.