C++ Programming - References - Discussion

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

17. 

Which of the following statements is correct?

  1. Pointer to a reference and reference to a pointer both are valid.
  2. When we use reference, we are actually referring to a referent.

[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 C

Explanation:

No answer description available for this question.

Santhoshi said: (Jul 17, 2013)  
int a=10;

int b=&a

int *p=&b; pointer to reference is possible.

Bhagat said: (Sep 21, 2013)  
/* Note: GCC Compiler (32 Bit Linux Platform). */

#include<iostream.h>
int main()
{
int *p;
int a=20;
int & b=a;
p=&b;// pointer to a reference
int * & ptr=p;// reference to pointer
cout<<"done";
return 0;
}

Adit Choudhary said: (Dec 11, 2015)  
Reference to an pointer.

int a=10;
int *p=a;
int *&r=p;

Nikhil P R said: (Sep 4, 2016)  
Only 2 is correct.

BeCause pointer to a reference is not valid.

Eg:
int a;
Int &b=a;
Int *p=&b;

Here it seems that p is a pointer to the reference but actually it's pointing to the same variable a.This is because a reference is automatically de referenced, &b internally becomes &*b. Thus in b what gets stored is the address of a.

Meet said: (Sep 13, 2016)  
B. Only 2 is correct.

Pointer to reference is illegal in C++. Pointer to reference is not allowed in C++.

Try following program:

include <iostream>
int main() {
int x = 10;
int *ptr = &x;
int &*ptr1 = ptr;
}
C++ compiler gives following error:
error: cannot declare pointer to 'int&'
int &*ptr1 = ptr;

So, please correct this mistake.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.