Difference between int f(int *i) and int f(int &i) [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Pointer vs. Reference
To do a call-by-reference in C++, I think I could use either of those two:
int f(int *x);
int g(int &x);
They would be called like so:
int *w;
f(w);
g(*w);
int y;
f(&y);
g(y);
Is there a difference in the functions f and g? I should be able to work with x as an int* pointer and *x as an int inside both functions. So what is the difference?

Most of this is a matter of taste. There is one important difference though. A pointer (*) can have a null value whereas a reference cannot be null and always must refer to a valid object.

The reference can't be NULL, so you don't have to check that. Otherwise, it's probably just syntactic sugar (at least for simple use cases). Check the disassembly of your program to see.

Related

"Type&" vs "Type*" in C++ function definitions [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 4 years ago.
I understand the pointer concept (int*), but I cannot understand int& because they give the same result. What does int &data exactly mean?
void add1(int *data){
(*data)++;
}
void add2(int &data){
data++;
}
int main()
{
int i=0;
add1(&i);
printf ("value i = %d\n",i) // show 1
add2(i);
printf ("value i = %d\n",i); // show 2
return 0;
}
The function signature:
void add2(int &data)
is the pass-by-reference facility in C++. It's a way to allow the function to change the value passed in and have that change reflected back to the caller. In C, upon which C++ was originally based, all paramaters are pass-by-value, meaning the functions get a local copy which, if changed, does not affect the original value passed in.
As an aside, pass by reference is probably the one facility I'd most like to see added to C (not full-blown C++ references, just in the function calls) since I'm pretty certain 42% of all C questions here on SO have to do with pointers :-)
The way to do pass-by-reference in C and C++ can be seen in the lines below. The first is the right way to do it in C++, and the second is the "pointer gymnastics" poor C developers have to go through to get the same effect:
void FortyTwo(int &x) { x = 42; } // Call with FortyTwo(variable);
void FortyTwo(int *pX) { *pX = 42; } // Call with FortyTwo(&variable);
In the latter, the pointer is passed by value but you can use that pointer to get at (and change) the data it points to. In C++, you should, as much as possible, avoid passing the pointers to values you want to change since that's one of the reasons references were added to C++ in the first place. It also makes your code easier to understand if you don't have to pepper it with pointer dereferences.

What does "void f(int (*)[7]) {}" mean? [duplicate]

This question already has answers here:
what does this mean char (*(*a[4])())[5]?
(4 answers)
Closed 7 years ago.
Can somebody explain what does it mean?
void f(int (*)[7]) {}
There is a site devoted to dealing with C gibberish and converting it to English:
cdecl.org Try it) It is convenient and sometimes even entertaining.
It states that void f(int (*)[7]) means: declare f as function (pointer to array 7 of int) returning void.
This is a function definition, which can accept a pointer to an array of int with 7 size.
Declare an 7-size array, int a[7];, and you can pass the address of it into f, like f(&a);
Check the live: http://cpp.sh/8ztz
It's definition of function that takes one parameter. That parameter is an unnamed pointer to array of ints.

Difference between func(int& x) and func(int &x) in C++ [duplicate]

This question already has answers here:
Difference Between Reference Sign After Data Type or Before Variable Name?
(3 answers)
Closed 7 years ago.
I'm trying to learn the basics of C++. The book I'm reading uses the following syntax:
func(int& x);
On the internet, I mostly see the following syntax:
func(int &x);
Both seem to do exactly the same. Is there any difference?
Literally no difference. Just a stylistic preference. The same is true of where you put the pointer.
See the answer to the Is int* p; right or is int *p; right? FAQ on Bjarne Stroustrup's website. It's for pointers, but what he writes equally holds for references:
Both are "right" in the sense that both are valid C and C++ and both
have exactly the same meaning. [...] The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis.
I prefer int& x. This way the reference becomes part of the type, it's an "int reference" called x, instead of "int, reference of x"
I find that thinking of "int reference" as a discrete type makes it less confusing, but to the compiler it's the same thing.

C++ pointer declaration [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C, what is the correct syntax for declaring pointers?
In C++ What is the difference between:
int* a;
and
int *a;
Is it same?
Yes. Those two constructs are identical. int *a; is more C style, because it is consistent with the "declaration follows use" rule in C. This rule means that you can read *a, and know that it gives you an int.
In C++, types get used on their own more often, so int* a; is more typical, as it puts the emphasis on they type being int*. Conformance to "declaration follows use" is less important in C++, because does not work everywhere anyway (it doesn't work with references, for example).
Note that if you write int* a, b; (which is the same as int *a, b;), then only a is a pointer.
They are the same. There is no difference. Also same as
int * a;
Those are the same. You can put the asterisk (*) anyplace.

Pass by pointer & Pass by reference [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Are there benefits of passing by pointer over passing by reference in C++?
In both cases, I achieved the result.
So when is one preferred over the other? What are the reasons we use one over the other?
#include <iostream>
using namespace std;
void swap(int* x, int* y)
{
int z = *x;
*x=*y;
*y=z;
}
void swap(int& x, int& y)
{
int z = x;
x=y;
y=z;
}
int main()
{
int a = 45;
int b = 35;
cout<<"Before Swap\n";
cout<<"a="<<a<<" b="<<b<<"\n";
swap(&a,&b);
cout<<"After Swap with pass by pointer\n";
cout<<"a="<<a<<" b="<<b<<"\n";
swap(a,b);
cout<<"After Swap with pass by reference\n";
cout<<"a="<<a<<" b="<<b<<"\n";
}
Output
Before Swap
a=45 b=35
After Swap with pass by pointer
a=35 b=45
After Swap with pass by reference
a=45 b=35
A reference is semantically the following:
T& <=> *(T * const)
const T& <=> *(T const * const)
T&& <=> [no C equivalent] (C++11)
As with other answers, the following from the C++ FAQ is the one-line answer: references when possible, pointers when needed.
An advantage over pointers is that you need explicit casting in order to pass NULL.
It's still possible, though.
Of the compilers I've tested, none emit a warning for the following:
int* p() {
return 0;
}
void x(int& y) {
y = 1;
}
int main() {
x(*p());
}
In fact, most compilers emit the same code for both functions calls, because references are generally implemented using pointers.
Following this logic, when an argument of (non-const) reference type is used in the function body, the generated code will just silently operate on the address of the argument and it will dereference it. In addition, when a call to such a function is encountered, the compiler will generate code that passes the address of the arguments instead of copying their value.
Basically, references and pointers are not very different from an implementation point of view, the main (and very important) difference is in the philosophy: a reference is the object itself, just with a different name.
References have a couple more advantages compared to pointers (e. g. they can't be NULL, so they are safer to use). Consequently, if you can use C++, then passing by reference is generally considered more elegant and it should be preferred. However, in C, there's no passing by reference, so if you want to write C code (or, horribile dictu, code that compiles with both a C and a C++ compiler, albeit that's not a good idea), you'll have to restrict yourself to using pointers.
Pass by pointer is the only way you could pass "by reference" in C, so you still see it used quite a bit.
The NULL pointer is a handy convention for saying a parameter is unused or not valid, so use a pointer in that case.
References can't be updated once they're set, so use a pointer if you ever need to reassign it.
Prefer a reference in every case where there isn't a good reason not to. Make it const if you can.
Here is a good article on the matter - "Use references when you can, and pointers when you have to."
Use references all the time and pointers only when you have to refer to NULL which reference cannot refer.
See this FAQ :
http://www.parashift.com/c++-faq-lite/references.html#faq-8.6