-2

I read tutorials about int* and int&. first, I type

int a = 3 

and then,

int* x = &a; 

means x is a pointer to the address of a which means the VALUE of x is something that POINTS to the address of a. Correct?

but if I do

int& x = a; 

that means the ADDRESS of x is pointing to the VALUE of a right? or does it mean something different? what EXACLTY does it mean?

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Use single backticks to highlight code, like so: \`int a = 3\` becomes `int a = 3` – Pezo Nov 04 '18 at 15:39
  • Think of references as *aliases* for the referenced variable. A reference *is* the referenced variable, just with a different name. – Jesper Juhl Nov 04 '18 at 15:40
  • A pointer **points at** the thing, not at the thing's address. So `x` Is a pointer to `a`, that is, the value of `x` is the address of `a`. – Pete Becker Nov 04 '18 at 16:34
  • @Pete Becker the fact that we assign &a to 'int* x' suggests that value of x is a pointer that points to &a in which case, &a is the address of a, not the VALUE of a. – Python_Beginner Nov 04 '18 at 17:11
  • @Python_Beginner -- `&a` is the address of `a`. Storing that value in a pointer sets the pointer to that value, just like `int i = 3;` stores 3 in `i`. – Pete Becker Nov 04 '18 at 17:20
  • @Pete Becker Then why is it that if I print out x, it shows 00x7ffeefbff5c8, and if I wanna see 3 printed out, I have to deference x ? – Python_Beginner Nov 04 '18 at 21:09
  • @Python_Beginner — that’s how pointers work. Get yourself a good textbook. – Pete Becker Nov 04 '18 at 21:14
  • @Pete Becker I asked the same question on the post below and according to the answer I got, you're wrong. so... not sure what to say. – Python_Beginner Nov 04 '18 at 22:55
  • @Python_Beginner — get yourself a good textbook. – Pete Becker Nov 04 '18 at 23:05
  • Also see [How C++ reference works](https://stackoverflow.com/q/7418483/608639) and [How is reference implemented internally?](https://stackoverflow.com/q/3954764/608639) – jww Nov 05 '18 at 10:01

2 Answers2

1

The two & signs in different places mean two different things.

In int &x = a, the & is part of the type of x, which is int& or reference to int.

When you do int *x = &a, the & is the address-of operator, which is a unary operator that returns the address of the object it's applied to (just like -a would return the negative of a).

For further reading look at the question JeJo mentioned: What are the differences between a pointer variable and a reference variable in C++?

Pezo
  • 1,458
  • 9
  • 15
  • wow... I didn't know that... so reference and address are two completely different things... and that in 'int &x = a', & means reference and in 'int *x = &a', & means address. Thank you! – Python_Beginner Nov 04 '18 at 16:17
  • just to double check, if I type 'int& x = a', that means I'm assigning the VALUE of a to the REFERENCE of x right? – Python_Beginner Nov 04 '18 at 16:55
  • No, you declare a reference to `int` named `x` that refers to `a`, `x` becomes another name for `a` if you will. – Pezo Nov 04 '18 at 17:38
  • I see. that makes sense. – Python_Beginner Nov 04 '18 at 21:04
  • and the last question... if I type 'cout << x;' , it prints out 00x7ffeefbff5c8. if I type 'cout << *x;' , it prints out 3. that means x which is a pointer is pointing to an ADDRESS right? and tha'ts why if I dereference it, it shows the value which is 3, right? – Python_Beginner Nov 04 '18 at 21:11
  • Yes that's right. – Pezo Nov 04 '18 at 21:12
0

One other way to understand this is to create two variables and set both values to 3. Then, print the addresses of both variables and compare them to the address of the int &y = a;

int main()
{
 int a = 3; int* x = &a;
 int b = 3; int* m = &b;

 cout<<"x : "<<x<<endl;

 int& y = a;
 cout<<"y : "<<y<<endl;

 int* z = &y;
 cout<<"z : "<<z<<endl;

 cout<<"m : "<<m<<endl;

 return 0;
}

You can see that the first the two addresses are the same, as against the address of variable b.

KudmiSubba
  • 59
  • 2
  • 9