Home > Blockchain >  Interpreting the & sign in C
Interpreting the & sign in C

Time:07-25

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!

CodePudding user response:

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

CodePudding user response:

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.

  • Related