0

My confusion:

Since both the pointers and references use the symbol &, and in pointers & symbol is usually interpreted as "the address of ...", I wonder whether it should be interpreted the same in references.

My theory:

When & sign appears on the right hand side of =, it can be interpreted as "the address of".
When & sign appears on the left hand side of =, it should be associated with references and CAN'T be directly interpreted as "the address of".

My proof:

  • & in pointers:
int a = 1;     
int *p = &a;    

The value of pointer variable p is the address of variable a.
In other words, p is a pointer pointing to variable a.

  • & in references:
int a = 1;
int & b = a;

b is a reference to a.
In other words, the address of reference variable b is the same as the address of variable a.
But the code itself wouldn't be appropriate to directly interpreted it as "the address of reference variable b is the address of variable a", because otherwise then the code should instead be

int a = 1;
int & b = &a;

, and that is incorrect according to C++ syntax.

Error message:

error: cannot bind rvalue '(int)(& a)' to 'int&'

Thanks for replies in advance!

Ross Li
  • 13
  • 2
  • 2
    Yes, there are at least two unrelated meanings: "a reference" (when in types), and "address of" (when in expressions). Don't try to reconcile those. – HolyBlackCat Jul 25 '22 at 06:31
  • Does this answer your question? [ampersand '&' operator at end of parameter](//stackoverflow.com/q/19555289/90527) – outis Jul 25 '22 at 06:43
  • "I wonder whether it should be interpreted the same in references" - no. – 463035818_is_not_an_ai Jul 25 '22 at 06:54
  • its more like with `*` that can be multiplication `3*2` or denote a pointer type `int* x` (or the dereference operator `int y = *x;`). – 463035818_is_not_an_ai Jul 25 '22 at 06:56
  • Counterexamples (not that they'd show up in production): `templatefoo(T=0); foo();`, `int a=0; const long& b = static_cast(a);` – outis Jul 25 '22 at 07:16
  • This is explained in any beginner level [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and also in the dupes listed. In particular, what `&` mean in different contexts. – Jason Jul 25 '22 at 07:19
  • More realistic counterexample: `auto transform = [](const auto& x) { return x; };` – outis Jul 25 '22 at 07:20
  • Or even simply `void foo(int& x) {}`. – outis Jul 25 '22 at 07:28

2 Answers2

1

When you use the reference operator for this code block

int a = 1;
int & b = &a;

you are trying to assign an "address" into an "integer" variable which is not allowed. Pointers can have addresses inside them not integer variables. However when you use the reference operator like this

int a = 1;
int & b = a;

b is an integer variable which has the SAME address as a this is why you get the following error

PoePew
  • 37
  • 7
  • Thanks for the reply! But I wasn't really confused about the error but only the interpretation of & operator...... – Ross Li Jul 25 '22 at 06:51
0

Since there are only so many ASCII symbols, it's inevitable that they get re-used for contradictory purposes, and & is one such character.

It means either address of in a statement or reference to in a variable declaration. Thinking of the "right-hand-side" is close, but it's really just its presence in a statement or, more specifically, an expression. Remember the rules apply in function definitions as well, as in int f(int &a) where there's no RHS in play.

In your example:

int & b = a;

This reads as "b is a reference to a, which is an int".

Keep in mind references might seem similar to addresses as in pointers, but they are not at all the same. A reference is an alias, as in it is entirely equivalent to. A pointer is, by definition, always one degree removed.

That is in the case of:

b = 2;

This assigns directly to b, which is an alias for a, so they both change. To adjust a pointer int *c = &a you would need to do *c = 2 which changes a but does not change c, the address of a remains the same.

Remember you can have references to pointers, pointers to references, and any combination you can dream up, even from nightmares, like int &*&**&***a if you so wish. It's valid! (Just not recommended.)

So things to keep in mind for a simple example of int &b = a:

  • b and a share the same address
  • Modifying b always modifies a
  • To the compiler, b is just an alias for a
  • b[0] is a syntax error unless a can be indexed as an array
  • *b is a syntax error unless a can be de-referenced as pointer

Whereas for int *c = a:

  • c has a different address from a
  • c is a different variable from a
  • Modifying c directly does not modify a
  • Modifying a de-referenced *c does modify a
  • c behaves like an array, as in c[0] can be used to fetch or modify a
  • Pointers generally incur an additional level of overhead when de-referencing and "exercising" them

If you're ever wondering what's going on, look at the assembly output of a simple program that uses both pointers and references. The differences can be substantial.

tadman
  • 208,517
  • 23
  • 234
  • 262