This question already has answers here:
how does the ampersand(&) sign work in c++? [duplicate]
(3 answers)
Closed 6 years ago.
I have this function:
void fun(int x, int& sum)
{
sum = x * x;
}
what is wrong with:
fun(4, &y);
fun(4,5);
&y is not a reference. It is a pointer to variable y. If you want to pass it by reference you just pass it like so:
fun(4, y);
In this function call
fun(4, &y);
argument y has type of pointer while the corresponding parameter is reference to int.
If y is defined like
int y;
then the valid function call will look like
fun(4, y);
This function call is invalid
fun(4,5);
because the second argument is a temporary expression that is not lvalue. Thus it may be bound to a constant reference. However the corresponding parameter is declared as a non-constant reference because the parameter is changed in the function. Thus the compiler will issue a diagnostic message.
Related
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.
This question already has answers here:
Passing an array by reference
(5 answers)
Closed 6 months ago.
void f(int, const int (&)[2] = {}) { } // #1
void f(const int&, const int (&)[1]) { } // #2
In those function overloads but what exactly are the second parameters.
My guess is,
#1 any empty array
#2 Returns the reference of the first index of an array
Both are references to arrays of the written type and size.
For example const int (&)[2] is a const lvalue reference to an array of 2 int (or equivalently a lvalue reference to an array of 2 const int).
The first one has a default argument which would create a temporary array of the given type to which the reference binds if no argument is provided in a call. That temporary array would be initialized as if by a = {} initializer. (In the case of type int this means the elements are zero-initialized.)
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.
This question already has answers here:
Default argument for structure reference in C++
(5 answers)
Closed 9 years ago.
it is possible for a function to not have some struct info? Something like this
int f(int x,int y=1) and calling f(2) it's just fine but for this int f(int x,struct my_struct &y)
what's the correct way considering that it's a reference there?
Since it is a non-const reference, you can't bind it to a default value which is a temporary object. You could bind it to some non-temporary object like so:
my_struct obj;
int f(int x, my_struct& y = obj) {
// ...
}
But this would mean that every invocation of f without a second argument would have access to the same obj. They could each modify it and then the next would receive an obj with a different state. This isn't very typical of default values. You expect it to have the same default value each time.
Why would your function take an object by non-const reference if it were possible to not pass an object? You take a non-const reference when you want the caller to pass an object for you to modify.
It may make more sense for your y parameter to be a const reference instead. You take a const reference when you want the caller to pass you an object for you to observe and you don't want to copy it. Then you could bind it to a temporary object like so:
int f(int x, const my_struct& y = my_struct(1, 2, "some args")) {
// ...
}
This question already has answers here:
In C++, what does & mean after a function's return type?
(11 answers)
Closed 10 years ago.
In this piece of code, why f() is declared as "double & f(..."? What does it mean and how does it work? I don't even know what to google to find the answer to my question. Please help.
double a = 1, b = 2;
double & f (double & d) {
d = 4;
return b;
}
I know ampersand sign means the address of a variable or a function, but I don't see why it would make sense to write it when you are declaring a function.
Consider these two functions: power2() and add():
void power2 (double& res, double x) {
res = x * x;
}
double& add (double& x) {
return ++x;
}
The first computes the power of x and stores the result in the first argument, res, – it does not need to return it.
The second returns a reference, which means this reference can later be assigned a new value.
Example:
double res = 0;
power2(res, 5);
printf("%f\n", res);
printf("%f\n", ++add(res));
Output:
25.000000
27.000000
Please note that the second output is 27, not 26 – it's because of the use of ++ inside the printf() call.
When the & operator is used in a declaration form, preceded by a type it doesn't mean "the address of" but a "reference to" which is essentially an automatically dereferenced pointer with disabled pointer arithmetic.
There are no references in C, so if you want to pass or return by reference, you had to pass a const pointer and dereference it to access the pointed to value. C++ added references to make this concept easier, to prevent accidental walking off the address with pointer arithmetic, and to save the need to dereference the pointer. This makes working with it much easier and resulting in cleaner and more readable syntax.
In this case, the ampersand does not mean taking an address, but it denotes a reference. Here, f is a function that takes a reference to double as parameter and returns a reference to double.
You might want to read about C++'s references in your textbook of choice, since they are a very basic part of the language.