17

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?

1

8 Answers 8

13
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. 
Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

also, you're saving yourself the trouble of checking for nullptr.
4

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 } 

2 Comments

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.
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.
3

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

Comments

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.

Comments

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.

3 Comments

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.
@ John I never said to call the with function with &a, I'm stating the meaning of &a in either function declaration or definition.
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.
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.

Comments

-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'.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.