18

Possible Duplicate:
Difference between pointer variable and reference variable in C++

As I am starting with C++, I found the operation below confusing. I got to know about passing by reference and passing by value. But recently I came across functions like this which confused me:

Func1(int &a)
Func2(int *a)

Both of the functions expect the address of a, but when I call Func1 I do that by Func1(a) and in case of Func2, I call by Func2(&a).

How come Func1 is accepting int a directly while it is expecting the address of a?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • Related: *[Pass by reference and value in C++](https://stackoverflow.com/questions/410593/pass-by-reference-value-in-c)* and [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value#373429) – Peter Mortensen Aug 16 '22 at 13:59

8 Answers8

14
Func1(int &a)
// It accepts arguments by reference.
// Changes to an inside Func1 are reflected in the caller.
// 'a' cannot bind to an Rvalue, e.g., it can't call Func1(5)
// 'a' can never be referring to something that is not a valid object

Func2(int *a)
// It accepts arguments by value.
// Change to an inside Func1 is not reflected in the caller, and changes to *a are
// 'a' can bind to an Rvalue, e.g., Func1(&localvar).
// 'a' can be NULL. Hence Func2 may need to check if 'a' is NULL.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
7

When providing an argument for a pass-by-reference parameter, the compiler will do the necessary & operation behind the scenes. You, as the programmer, know that it is internally using the address of your argument, but this is hidden in the pass-by-reference abstraction. This is safer than using a pointer as you cannot inadvertently reassign a reference.

asdfjklqwer
  • 3,536
  • 21
  • 19
5

Internally, there's not much difference. But one is a reference, and the other one is a pointer. The primary difference is that you can't modify the reference in your function, so the reference will always point to a.

I.e.,

void func1(int &a) {
    a = 5;
}

This will modify a, i.e., whichever variable the caller pointed to.

void func2(int *a) {
    *a = 5;   // Same effect as the above code
    a = &b;   // You couldn't do that with a reference
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 1
    `a = &b` only changes `a` local to the function. You would need a pointer to a pointer to change it once it leaves the function. – Firedragon Feb 07 '12 at 13:46
  • 1
    That's not the point. You could do another `*a = 5` afterwards, which would modify a different address. That's not possible with a reference. – EboMike Feb 07 '12 at 18:24
4

Basically, when you have a &something you have a reference and this is nothing more than a pointer which cannot change. In other words, it is a const pointer, so basically it’s like *something, but in this case you can change the pointer (to point somewhere else) :)

A quick example:

Reference: Object &obj

The same written with pointer syntax: Object* const obj

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marek Szanyi
  • 2,348
  • 2
  • 22
  • 28
1

Func1 will take a reference to an int, and Func2 will take a pointer to an int. When you do Func2(&someint), you're giving the function the address of someint. This address can be dereferenced to get its value. When you "pass by value" Func1(int someint), then, a copy of someint is performed. When you pass either by reference, or by pointer, no such copy is performed.

A reference can be thought of, as essentially an "alias" to the original value, or, another way of referring to it. Yes, it's abstract, but, everything else is implementation-specific and not for you to worry about.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
please delete me
  • 711
  • 2
  • 9
  • 17
0

In Func1(int &a), &a is the reference to the variable that you will be passing.

In Func2(int *a), *a is the pointer to address of the variable that you will be passing by its address.

cpx
  • 17,009
  • 20
  • 87
  • 142
  • What? Func1(int &a) is the function declaration. You're declaring a function that will take a reference to an int. When calling Func1, you would not say int &a, you would say &a, in which case, yes, you would get the address of the int(and this would be invalid with the above declaration, because the function takes a reference to an int, not an intptr). Can't believe this actually got two upvotes. – please delete me Nov 22 '10 at 06:39
  • @ John I never said to call the with function with &a, I'm stating the meaning of &a in either function declaration or definition. – cpx Nov 22 '10 at 06:53
  • Except, in the function declaration, int &a means you'll be passing in a reference-to-int to the function. &a means "address of a" only when used outside function declarations. – please delete me Nov 22 '10 at 07:02
0

When the function definition is Func1(int &a), it means this function will accept the address of the variable which will pass to this function as a parameter. (So you don't need to take care of passing the address of the variable which will a parameter to the function). The function default behavior will be to get the address of the passed variable.

E.g.,

Func1(int &a){
   a = 5; // 'a' will store 5 at &a
}

Func1(a) // We don't need to pass &a. The function definition will take care of this.

===================================================================

Whereas if the function definition is Func2(int *a), it means it will hold the address of the given value. That means you must have to pass &a in the function call as a parameter, which will be later stored as in *a in the function definition.

E.g.,

Fun2(int *a){
   *a = 7; // a will store 7 at &a
}

Function call: Fun2(&a); Must have to pass &a. The function definition will not take care of this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Poonam Bhatt
  • 10,154
  • 16
  • 53
  • 72
-1

It is not like that.

  1. Func1(int &a) - when you call this function, Func1(a) which will pass the int only there, the function receives the address of the passing argument.

  2. Func2(int *a) - when you call this function with Func2(&a), this statement just passes the reference of 'a'. In the called function argument '*a' which will gain the value which is referring that calling function's parameter '&a'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84