The ambiguous star operator on function name [duplicate] - c++

This question already has answers here:
How does dereferencing of a function pointer happen?
(5 answers)
Closed 8 years ago.
Just a minimum working example:
#include <stdio.h>
void foo(char* str)
{
printf("%s\n", str);
}
main()
{
foo("foo");
(* foo)("* foo");
}
which outputs
foo
* foo
I thought the function name should be the address of the routine in the code segment, so the star operator should return an executable instruction. But how does this operator work here actually?
Thanks for shedding some light.

In C, functions are not values, you cannot "dereference a pointer to a function" and get something (a value) that makes sense.
Therefore, trying to dereference and call such a pointer has no additional effect, it's the same as calling through the pointer in the first place.

Related

Over load a function by value and by reference [duplicate]

This question already has answers here:
Ambiguous Reference/Value Versions of Functions
(4 answers)
Overload resolution between object, rvalue reference, const reference
(1 answer)
Ambiguous call with overloaded r-value reference function [duplicate]
(1 answer)
Closed 3 months ago.
Please refer to the below simple scenario:
void test(int k){
cout<<"Passed as value"<<endl;
cout<<k<<endl;
}
void test(int &k){
cout<<"Passed as reference"<<endl;
cout<<k<<endl;
}
int main(){
int x = 10;
test(x);
}
As far as my understanding is concerned, when you call a function, at that time the compiler has no way of knowing if the variable passed to it is by value or by reference. Thus, I understand why the compiler gives the error that call of overloaded ‘test(int&)’ is ambiguous. Is there any way that we can specify while calling the function itself that I intend to pass the value as reference?" N.B. I already know the work around this issue by passing the variable as address.

What does the * symbol mean after void. What does adding void* do for the function? [duplicate]

This question already has answers here:
What does void* mean and how to use it?
(10 answers)
Closed 1 year ago.
If void* makes a pointer that holds the address of any type, what does adding void* to the start of the function mean, why not just put void?
void* myFunction(void* arg)
{
int thread_id = *((int*) arg);
void *myFunction() means that myFunction returns "pointer to void", i.e. it returns a pointer, for which the type pointed to is not specified.
void myFunction() means that myFunction returns "void", i.e. it returns nothing at all.

why the function pointer, function address and function are same? [duplicate]

This question already has answers here:
Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?
(5 answers)
Closed 7 years ago.
#include <stdio.h>
int add(int a, int b)
{
int c =a+b;
return c;
}
int main()
{
int a=20,b=45;
int (*p)(int , int);
p=&add;
printf("%d\n%d\n%d\n\n",*add,&add,add);
printf("%d\n%d\n%d\n\n",*add+1,&add+1,add+1);
return 0;
}
Outupt is
4199392
4199392
4199392
4199393
4199393
4199393
So why the *add, &add, add are same?
I also doubt that 'add' act like an array, correct me if I am wrong, because, address of array and array itself are same.
In C the only things you can do with a function is to call it or taking its address. So if you aren't calling it, you're pretty much taking its address.
"C11 §6.5.6 Additive operators" /6 discusses adding to a pointer
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, ... . If the result points one past the last element of the array object, ...
Nowhere does the C spec define adding an integer type to a function pointer. Thus undefined behavior.

Is there any advantage of using std::addressof() function template instead of using operator& in C++? [duplicate]

This question already has answers here:
When to use addressof(x) instead of &x?
(5 answers)
Closed 7 years ago.
If addressof operator& works well then why C++ has introduced addressof() function? The & operator is part of C++ from the beginning - why this new function is introduced then? Does it offer any advantages over C's & operator?
The unary operator& might be overloaded for class types to give you something other than the object's address, while std::addressof() will always give you its actual address.
Contrived example:
#include <memory>
#include <iostream>
struct A {
A* operator &() {return nullptr;}
};
int main () {
A a;
std::cout << &a << '\n'; // Prints 0
std::cout << std::addressof(a); // Prints a's actual address
}
If you wonder when doing this is useful:
What legitimate reasons exist to overload the unary operator&?

When to use function reference instead of function pointer in C++? [duplicate]

This question already has answers here:
Function References
(5 answers)
Closed 8 years ago.
When to use function reference such as
void (&fr)() = foo;
fr();
instead of function pointer such as
void (*fp)() = &foo;
fp();
Is there something function pointer can't do but function reference can?
when you define a reference:
void (&fr)() = foo;
fr();
it gives you the ability to use fr almost everywhere where it would be possible to use foo, which is the reason why:
fr();
(*fr)();
works exactly the same way as using the foo directly:
foo();
(*foo)();
One more Difference is dereferencing a function reference doesn't result in an error where function pointer does not require dereferencing.