-3

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
  • 2
    `int* &a = y;` is a syntax error, if you don't see an error message then you're doing something wrong with your compiler. I guess you are actually using a compiler for some other language that is not C – M.M Jan 14 '19 at 03:41
  • 1
    @M.M `int* &a = y;` defines a reference to a pointer and assigns `y` to it? – duong_dajgja Jan 14 '19 at 03:43
  • 1
    @duong_dajgja no, it is a syntax error – M.M Jan 14 '19 at 03:43
  • @M.M is it valid in C++ then? How to declare a reference to a pointer in C btw? – duong_dajgja Jan 14 '19 at 03:45
  • 1
    @duong_dajgja this is a C question, remarks about c++ are irrelevant – M.M Jan 14 '19 at 03:48
  • If you're doing `c++`, you're using references [by virtue of having `&` on the left hand side]. The first three would be valid. The last would need `int &a = x;` But, if you're doing `c` [which does _not_ have references per se], you probably want: `int x = 12; int* y = &x; int* a = y; int* b = &x;` – Craig Estey Jan 14 '19 at 03:55
  • 2
    You are expecting the third line to create a reference to the `y` object. What object are you expecting the fourth line to create a reference to? – David Schwartz Jan 14 '19 at 04:09
  • You aren't dereferencing any pointers either. – M.M Jan 14 '19 at 04:23
  • This works: `int** p1 = &y;` and this fails: `int** p2 = &&x;` (In both C and C++). So the failure you asked about is no surprise at all. What should be a surprise is that `int* const& c = &x;` works – Ben Voigt Jan 14 '19 at 04:25
  • `int *&& b = &x;` is also fine – M.M Jan 14 '19 at 04:26
  • From the comment section I assume OP is working with C and put C in the tag originally? C doesn't have the concept of "reference" so you cannot declare a reference. If you want reference, use C++. – texasbruce Jan 14 '19 at 04:33
  • Please read https://stackoverflow.com/help/mcve . When you post questions on this site, it is helpful to those who want to help you if they can copy/paste your posted code in _its entirety_ and compile it successfully. – StoneThrow Jan 14 '19 at 04:37

2 Answers2

0

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?

StoneThrow
  • 3,821
  • 11
  • 42
  • ...or use an rvalue reference – M.M Jan 14 '19 at 04:40
  • Can you tell me what does `int *&a `, in `int *&a= y` mean? I am referring to the original code that I have written. – StickyCoffee Jan 14 '19 at 04:41
  • @StickyCoffee - See my updated answer RE: reading variable declarations right-to-left to understand constness and what points to/refers to what, and see if that helps. – StoneThrow Jan 14 '19 at 04:56
  • Just one last question - what is the difference between int * p, and int *&y - is there a difference. – StickyCoffee Jan 14 '19 at 05:27
  • @StickyCoffee `int*` is a variable of type `integer pointer`. `int*&` is a _reference_ to a variable of type `integer pointer`. It's helpful to think of references and variables distinctly: variables exist "on their own": you can simply declare a variable, e.g. "`int x;`". References _must refer to a variable_: you can't just declare "`int*& y`" - `y` must refer to a variable; in this case it must refer to a variable of type `integer pointer`. Hope that helps. – StoneThrow Jan 14 '19 at 18:52
0

In C, there is no concept of reference so the 3rd and 4th lines won't work in C.

The 3rd line is valid C++ code.

The 4th line is not valid because when you take the address of x using &x, you create a rvalue of int*. You cannot bind an rvalue to a lvalue reference (in this case &b). A lvalue reference must be bound to lvalue (which is what your 3rd line does). You can assign the rvalue to an rvalue variable, bind it to a const lvalue reference, or bind it to an rvalue reference (since C++11):

int* b = &x;  //assign rvalue to a variable
int* const &b = &x; //bind rvalue to const lvalue reference
int* &&b = &x; //bind rvalue to rvalue reference
texasbruce
  • 13,867
  • 12
  • 57
  • 117