C++ Programming - References - Discussion
Discussion Forum : References - General Questions (Q.No. 9)
9.
Which of the following statements is correct?
- An array of references is acceptable.
- We can also create a reference to a reference.
Discussion:
13 comments Page 1 of 2.
Ajit Kumar Sharma said:
1 year ago
@All.
In C++, I checked the above two different statements for representing the way to write the reference to reference in this discussion page . And complied it both statements. Both giving me the same results and working fine to run.
For example, int x = 4;
i.e , (1) int &(&y) = x; // correct and run without error.
(2) int x= 4;
int& y = x;
int& z = y; // correct and run without error.
Note : 'x' variable in both statements is already declared as lvalue.
In C++, I checked the above two different statements for representing the way to write the reference to reference in this discussion page . And complied it both statements. Both giving me the same results and working fine to run.
For example, int x = 4;
i.e , (1) int &(&y) = x; // correct and run without error.
(2) int x= 4;
int& y = x;
int& z = y; // correct and run without error.
Note : 'x' variable in both statements is already declared as lvalue.
Prathamesh mukkawar said:
5 years ago
According to me;
The possible one is;
#include<iostream>
using namespace std;
int main()
{
int i = 11;
int &j = i;
int &k = j;
cout<<"i = "<<i<<endl;//11
cout<<"j = "<<j<<endl;//11
cout<<"k ="<<k<<endl;//11
return 0;
}
The possible one is;
#include<iostream>
using namespace std;
int main()
{
int i = 11;
int &j = i;
int &k = j;
cout<<"i = "<<i<<endl;//11
cout<<"j = "<<j<<endl;//11
cout<<"k ="<<k<<endl;//11
return 0;
}
Pooja said:
6 years ago
No, we can also create a reference to reference but we cannot create an array of references.
(1)
Abhijeet said:
6 years ago
The reference to a reference i.e.
int &(&ref2)=ref;
So, not acceptable.
int &(&ref2)=ref;
So, not acceptable.
Jaideep said:
7 years ago
You cannot create a reference to a reference, the examples stated in the discussion are all rvalue references.
Debajyoti Dev said:
8 years ago
Wrong, reference to reference can be created.
int a = 7;
int &b = a;
int &c = b;
c = 10;
cout << a;
int a = 7;
int &b = a;
int &c = b;
c = 10;
cout << a;
(1)
Sagar said:
8 years ago
@ALL.
My explanation is;
int a =10;
int &r=a; // r is a reference of a
int &p=r;
cout<<"a = "<<a <<" &a = "<<&a<<endl;
cout<<"r = "<<r <<" &r = "<<&r<<endl;
cout<<"p = "<<r <<" &p = "<<&r<<endl;
And the output is =
a = 10 &a = 0x7fff53f201a4
r = 10 &r = 0x7fff53f201a4
p = 10 &p = 0x7fff53f201a4.
My explanation is;
int a =10;
int &r=a; // r is a reference of a
int &p=r;
cout<<"a = "<<a <<" &a = "<<&a<<endl;
cout<<"r = "<<r <<" &r = "<<&r<<endl;
cout<<"p = "<<r <<" &p = "<<&r<<endl;
And the output is =
a = 10 &a = 0x7fff53f201a4
r = 10 &r = 0x7fff53f201a4
p = 10 &p = 0x7fff53f201a4.
Swapnil said:
8 years ago
Reference to another reference can be created.
Khushboo kumari said:
1 decade ago
Reference is nothing but an alias of a variable i.e. another name given to some variable, it behaves like a constant pointer but is not a pointer, as pointer stores address of the variable and point to its value using '*'. Similarly reference also points to the value of some variable but it act as its another name and as a constant pointer, once initialized cannot refer any other value.
Sujit said:
1 decade ago
Reference to a reference cannot be created as reference is nick name given to that variable and reference doesn't have any memory like pointers which has 4 bytes of memory to store address of variables where it points.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers