C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.11)


Why reference is not same as a pointer?

[A]. A reference can never be null.
[B]. A reference once established cannot be changed.
[C]. Reference doesn't need an explicit dereferencing mechanism.
[D]. All of the above.

Answer: Option D


No answer description available for this question.

Chandan said: (May 19, 2012)  
References always stores the address of objects so it can not be null and it can not be changed due to only one reference is possible to one object.

Dinesh Kumar said: (Oct 14, 2012)  
cout is an object of class ostream that represents the standard output stream. It corresponds to the cstdio stream stdout.

Nusrat Jan said: (Nov 29, 2012)  
What is the difference between reference and pointer?

Vaibhav said: (Apr 3, 2013)  
A pointer can have Null value ,int *p=Null means, it is not having any address to point . while reference can't have &p=Null(not allowed).

2- address can be changed in case of pointer while in case of reference it can't b.

int a=10;
int *p =&a,
int b=30;

int *p=&b; valid now, but in reference it's not possible.

Joshua said: (Jan 26, 2014)  
Okay, so there's this idea that changing a reference is not possible. Somebody please explain this because, as I have written a program that does it, it is very clearly possible.

classA apples(10);
classA bananas(0);
classA &ref = apples;
ref = bananas;
std::cout << ref.num;

That code will print "0". This means that I have created a reference and assigned apples to it, whose member variable num = 10. I then reassign bananas to ref, and now it is a reference to bananas.

What am I missing here? I've seen 3 sources so far that say this isn't possible when.. it is.

Sachchidanand Kumar said: (Jul 4, 2014)  
What is difference between call by address and call by reference?

Badal Singh said: (Mar 8, 2015)  
In case of call by reference method, actual parameters are normal variables and formal parameters are reference variables whereas in case of call by address, actual parameters are addresses of normal variables and formal parameters are pointer variables.

Gitika said: (Sep 8, 2015)  
Answer to @Joshua.

When your assigning ref=bananas you are not creating a reference but when you are assigning it to apple then it create reference any change to apple will cause change to reference but any change to banana after assigning will not change reference.

Check it out with simple code:

int n=10,r=20;
int &a=n;
cout<<&a<<" "<< a<<endl;
cout<<&a<<" " <<a;

Now check output you will understand difference.

Ajay said: (Oct 28, 2015)  
Reference doesn't need an explicit dereferencing mechanism. Why?

Ritika said: (Sep 11, 2016)  
Please post an answer to @Ajay's question. Even I've got the same doubt.

Akash Shelke said: (Nov 6, 2016)  
Can pass the parameters inside a main?

Sruthi said: (Aug 2, 2017)  
Reference doesn't need an explicit dereferencing mechanism. Why?

Pallavi said: (Nov 3, 2017)  
You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.

Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time.

A reference must be initialized when it is created. Pointers can be initialized at any time.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.