C++ Programming - References - Discussion

Discussion Forum : 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.
Only 1 is correct.
Only 2 is correct.
Both 1 and 2 are correct.
Both 1 and 2 are incorrect.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
16 comments Page 1 of 2.

Abhijeet said:   6 years ago
#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.

AKAK said:   1 decade ago
- 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.

Prashant said:   8 years ago
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.

Harsha said:   8 years ago
#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?

Nidhi said:   1 decade ago
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

Bhasker Bamsiya said:   9 years ago
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].
(1)

Karthik said:   1 decade ago
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.

Vedi G said:   6 years ago
@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.

Sireesha said:   1 decade ago
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.

Yadav said:   1 decade ago
Pointer can change its reference, it is not a constant.

What does the second statement mean?

A reference is not a constant pointer.


Post your comments here:

Your comments will be displayed after verification.