-2

I am trying to declare a reference variable to a pointer.

#include<iostream>
using namespace std;
int main()
{
 int m = 10;
 int *p = &m;
 int & ref = p; // this is showing error
 }

I am reading a c++ book and in the book i saw this code, but this is showing an error. It is supposed to set ref as a reference variable to point to pointer p and in turn point to m.

daljinder singh
  • 177
  • 1
  • 9

2 Answers2

0

You are missing the asterisk * at the reference to pointer declaration:

int *p = &m;
int *&ref = p;
JFMR
  • 23,265
  • 4
  • 52
  • 76
0
#include<iostream>
using namespace std;
int main()
{
    int m = 10;
    int *p = &m;
    int *& ref = p; //read from right to left,so *& is a refrence to pointer
}
Li Kui
  • 640
  • 9
  • 21