What is the difference between & and * in C++ function arguments? [duplicate] - c++

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 2 years ago.
As an example I know that
void foo(int *num)
{
num++;
}
foo(&num);
will pass num by reference. However, why do people use
void foo(int &num)
{
num++;
}
I've heard that you should use that syntax over what I posted above, why is this?

Your first example passes by pointer, not reference.
The second example void foo(int &num) is a function that takes its argument by reference.

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.

How can I use "= operator" when function return? (in C++) [duplicate]

This question already has answers here:
C++ returning an assignment?
(1 answer)
Using assignment as a condition expression?
(5 answers)
What does assignment operator means in return statements, like return t =...?
(5 answers)
Closed 1 year ago.
I am starter of C++. When I study the opensource, I see the function.
int foo(int a, int b){
return a=b;
}
But I only know the function return such as...
int foo(int a, int b){
return a;
}
what is the meaning of this "return a=b" statement?

The ambiguous star operator on function name [duplicate]

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.

Passing a pointer by reference? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing a pointer by reference in c++
I have a function that needs to modify a pointer, ex:
bool someFunc(Something* something)
{
something = somethingElse;
return true;
}
The pointer is passed by value through and is not modified. How can I modify it?
Thanks
Just change the function signature to look like
bool someFunc(Something* &something)
and you'll get a modifiable pointer in someFunc().
bool someFunc(Something * &something)
bool someFunc ( Something * & something );
// ^ notice the reference symbol

Is there any advantage of using references instead of pointers in c++? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
For example, I have two functions that do the same thing:
int func(int &a)
{
return a+1;
}
and
int func2(int *a)
{
return *a+1;
}
What is the advantage of using func over func2 when calling any one of these functions?
A reference cannot be assigned null directly, but a pointer can be.
A reference cannot be reassigned to point to something else, but a pointer can be.
Both these can be advantages or disadvantages, depending on the situation.
Both of your functions are wrong. Since they don't modify the argument passed in, they should take them in as const, like this:
int func(const int &a)
{
return a+1;
}
int func2(const int *a)
{
return *a+1;
}
Now here's an advantage for references, I can pass rvalues into the reference version:
func(10);
func(func(func(10)));
I can't do that with the pointer version.
The pointer is more flexible, it can also be predefined in the declaration like:
int func2(int *a = nullptr);
Which does not work in your simple case but in many others it does.
The pointer may also more easily be used for other things, like storing in a list, typacasting it and other things.
And yes, the reference cannot be reassigned.
If a is an object that overloads standard C++ operators (e.g. operator[] or operator*), clients can use it in more standard C++ syntax like a[i], instead of a->operator[](i) or (*a)[i]