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

Community
  • 1
  • 1
Simsons
  • 11,743
  • 38
  • 145
  • 252

8 Answers8

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

Func2(int *a)
// accept arguments by value
// change to a inside Func1 not reflected in caller, 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
Chubsdad
  • 24,001
  • 4
  • 68
  • 121
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,406
  • 19
  • 19
5

Internally, there's not much difference. But one is a reference, 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
}
EboMike
  • 75,005
  • 14
  • 154
  • 164
  • 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 then a pointer which cannot change in other words a const pointer so basically its 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

Marek Szanyi
  • 2,328
  • 2
  • 22
  • 27
1

Func1 will take a reference to an int, 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 de-referenced 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.

Hope that helps.

please delete me
  • 713
  • 2
  • 8
  • 17
0

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 passing argument.

  2. Func2(int *a) - when yo 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 param '&a'.

Mohamed Saligh
  • 11,650
  • 18
  • 63
  • 83
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
  • 16,251
  • 19
  • 83
  • 139
  • 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 function definition is Func1(int &a), it means this function will accept address of variable which will pass to this function as parameter. (so you don't need to take care of passing address of variable which will parameter to function). Function default behavior will be to get address of passed variable.

e.g

 Func1(int &a){
   a = 5; //a will store 5 at &a
}
Func1(a) // don't need to pass &a, function definition will take care this.

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

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

e.g

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

function call: Fun2(&a); Must have to pass &a, function definition will NOT take care this.
Poonam Bhatt
  • 9,890
  • 16
  • 48
  • 70