-1

I'm looking at some c++ code that involves addresses and came across some lines that I don't understand.

#include <iostream>

int main()
{
    int x = 5;
    int &R = x;
    //int &R = &x;   // ERROR!

    std::cout << "R address: " << &R << "\n";
    std::cout << "R number: " << R << "\n";
}

The code works as expected, however I don't understand the line where the variable x is assigned to the address of R. My understanding is that this would be an error since you're assigning the value stored in x to an address. How come the line that is commented out returns an error?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Frantz Paul
  • 127
  • 2
  • 11
  • You can't declare R more than once. R is a reference to x already. – Ted Lyngmo Jan 19 '19 at 02:37
  • 3
    R is a reference, not a pointer. Think of references as pointers where you do not need to explicitly use `&` or `*`. – Andrew Sun Jan 19 '19 at 02:38
  • In that context (declaration/definition) the `&` applies to the `int` on the left not the `R` and it forms a *compound type* called "int reference". – Galik Jan 19 '19 at 02:55
  • Side note: `int *R = &x;` would make `R` a pointer to `int` and able to accept storing the address of an `int` (without jumping through some hoops I generally do not recommend) – user4581301 Jan 19 '19 at 03:07
  • 2
    The `&` has **two** different meanings. Learn the difference and you'll be fine. – Mark Ransom Jan 19 '19 at 04:00
  • Does this answer your question? [What's the ampersand for when used after class name like ostream& operator <<(...)?](//stackoverflow.com/q/1572016/90527) – outis Jul 25 '22 at 06:42

3 Answers3

1

I don't understand the line where the variable x is assigned to the address of R

The syntax of C++ can be confusing here, but what you see is not a variable being assigned to an address.

Here the ampersand has a different meaning: R is a variable of type int&, that is, a reference to an int. It doesn't mean "take the address of R". What it means, more or less, is that R is now an alias of x. It behaves like an int, not like a int* (pointer to int). If you print their value you'll find that it is the same, and if you change one of them you will also change the other.

You can have a look at this question for a comprehensive explanation of references.

1

One way to look at it is to move the reference sign from

int &R = x;

to

int& R = x;

R is an int& - a reference it an int, which means that R will not free the memory allocated by x when R goes out of scope, but it will be a reference to x while x lives. R is hereby declared and its declaration can not change, so if you try:

<type declaration> R = <value>

it must fail - since R is already declared.

But you can assign something to whatever R is a reference to

R = <value>

...as long as the value type is convertible to the type referenced by R (0-1 conversion operators away).

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

I don't understand the line where the variable x is assigned to the address of R

int &R = x;

The above line declares a reference-to-int named R, and makes it reference the already-existing variable x. i.e. in any code in the function below the above line, R will act as an alias for x.

//int &R = &x;   // ERROR!

The above line (when uncommented) produces an error because the value on the right side of the = is of type int * (i.e. it's a pointer-to-an-int), while the value on the left side of the = is of type int & (i.e. it's a reference-to-an-int).

In C++ references act like pointers in some ways, but as far as their syntax is concerned, they mostly follow the same rules as the type they are referencing -- this is because they are acting as aliases to a value of that type. So in this case, just as you can't assign a pointer to an int, you can't assign a pointer to an int & either:

int R = &x;   // This would also be an error
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234