Q:

Issues with int*x

I am having issue with dereferencing pointers
int x = 12;
int* y = &x;
int* &a = y;//This does work
int* &b = &x;//This does not work

Doesn't y and &x contain the same type of values / same value.
Can someone tell me what does int*& a actually represents.
Edit:1
Sorry, I did not realize that such a simple code would yield two different result in c and in c++. In c this line int*& a = y is simply a mistake. However, in c++, the code gives the error as I have shown.
Edit:2
int *&a = y; //compiles in g++ 7.3.0 ubuntu, but not in gcc 7.3.0

int *&b = &x; //it simply throws error in g++ and gcc

A:

In your posted code, &x is an rvalue, so if you want to capture a reference to it, you must capture it with a const reference.
What you need is int* const& b = &x.
The rule I learned, that still serves me well today, is to read a declaration right-to-left to understand the constness. In this case, you'd read something like "b is a reference to a const pointer to an int".
References must refer to variables (i.e. a named instance of a type). Const references may refer to temporary objects/nameless variables. E.g.
int x = 5; // x is a named instance of an int
int* p = &x;
int*& y = p; // the reference y is referring to the named variable p which is a pointer to an int.
int*& z = &x; // not ok because x is a named variable but "&x" is not, so a reference cannot refer to it.
int* const& w = &x; // ok because w is a const reference, not a reference.

Hope that helps. Spend some time online reading about references and const correctness and this will all gel. There are lots of great Q&A right here on those two topics.

This is a good article discussing lvalues and rvalues: What are rvalues, lvalues, xvalues, glvalues, and prvalues?

