C++ Programming - References - Discussion

Discussion Forum : 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.
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:
6 comments Page 1 of 1.

Shailja soni said:   7 months ago
I think only 2 is correct.

Meet said:   9 years ago
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.

Nikhil P R said:   9 years ago
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.
(1)

Adit choudhary said:   10 years ago
Reference to an pointer.

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

Bhagat said:   1 decade ago
/* 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;
}

Santhoshi said:   1 decade ago
int a=10;

int b=&a

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

Post your comments here:

Your comments will be displayed after verification.