C++ Programming - References - Discussion

Discussion :: References - General Questions (Q.No.2)

2. 

Which of the following statements is correct?

  1. Once a reference variable has been defined to refer to a particular variable it can refer to any other variable.
  2. A reference is not a constant pointer.

[A]. Only 1 is correct.
[B]. Only 2 is correct.
[C]. Both 1 and 2 are correct.
[D]. Both 1 and 2 are incorrect.

Answer: Option D

Explanation:

No answer description available for this question.

Nidhi said: (Jun 21, 2012)  
2 is correct
becuase a refrence can be thaught of as (*p) where we can not change p is true but a is value at that pointer
so a reference is a value_at-cannot_be_changed_by_us-pointer
and is not a constant pointer

Sireesha said: (Aug 23, 2012)  
You can always change the address being pointed by 'p' and also value at 'p' unless they are constant similar is the case with the reference variable.

Ashish said: (Sep 3, 2013)  
Can anyone please explain. Why the 2nd option is incorrect?

Karthik said: (Sep 4, 2013)  
2 is incorrect because reference once created alias i.e., initialized with the variable must not be assigned once again with the another new another. So, it seems like const pointer.

Yadav said: (Dec 23, 2013)  
Pointer can change its reference, it is not a constant.

What does the second statement mean?

A reference is not a constant pointer.

Chanchal said: (Nov 10, 2014)  
Pointer take the reference of another variable also and it is not a constant pointer because we can change it.

Akak said: (Apr 29, 2015)  
- Pointer has its own address. Reference (const or not) does not.

- Const pointer can be NULL. Reference can not.

- You can test for NULL pointer being returned by a function. You cannot test for NULL-reference, even if object being referenced went out of scope.

- You need to deference the pointer to access it's data, there is no deference a reference.

All that makes references conceptually and practically different from const pointers, even though under the hood they can be implemented as the same mechanism by a compiler.

Daniel Sandor said: (Nov 20, 2015)  
"The C++ standard does not force compilers to implement references using.

Daniel Sandor said: (Nov 20, 2015)  
"The C++ standard does not force compilers to implement references using pointer.

Bhasker Bamsiya said: (Apr 2, 2016)  
The Answer mentioned is wrong.

A reference variable can't refer to any other variable once it has been defined to refer to a particular variable.

The correct answer should be option [B].

Anu said: (Sep 25, 2016)  
I think the option 1 is correct.

Pooja said: (Sep 29, 2017)  
A is the correct answer.

Harsha said: (Oct 16, 2017)  
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout<<ref;
int y=40;
ref=y;
cout<<ref;
return 0;
}

Here reference is referred to another variable 'y' after referring 'x'. How 1st option is correct?

Prashant said: (Dec 31, 2017)  
When we are creating a reference to a variable we are actually giving it a different name so logically now ref and x are the same so whenever you are modifying ref you are modifying x and not changing what is ref referring to so the line.

ref=20 sets x also to 20 and line.
ref=y sets x equal to the value of y,
ref=y does not mean that ref is now a reference to y,
you can try printing x it will give you 40.

Therefore option 1 is incorrect.

Abhijeet said: (May 27, 2019)  
#include<iostream>
using namespace std;
int main()
{
int x = 10;
const int const *ptr=&x;
int& ref = x;
ref = 20;
cout<<"before change ref = "<<ref<<endl;
int y=40;
ref=y;
cout<<"after change ref = "<<ref<<endl;
cout<<"but it actually change x and not change only ref i.e.x = "<<x<<endl;
cout<<"add of x= "<<&x<<endl;
cout<<"add of ref = "<<&ref<<endl;
cout<<"add of ptr = "<<&ptr<<endl;
cout<<"ptr = "<<ptr<<endl;


getchar();
return 0;
}

As you can see,

A reference is not a constant pointer.
as pointer has its own address and ref does not.

Vedi G said: (Dec 8, 2019)  
@Anu.

Option 4 is correct because reference cannot have its own address. That's why its is not a constant pointer.

One time it hold one variable address only.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.