& in any situation points to an address in memory, whether it is from a regular variable or a pointer. |
& is the take the address of operator, when associated with a variable name, in other words it takes the address of that variable.
A
&
associated with a type, means it's a reference, which is just another name for an existing variable. The difference between a pointer and a ref is that a ref must refer to a variable, whereas a pointer can be made to point at anything including nothing
nullptr
Line 3 in the code means assign the address of tmp//regular variable to a pointer to myPtr |
Yes.
Now if I wanted to have another variable pointing to the same address through myPtr, it will have to be a double pointer given the error. |
The error came from
5 6 7
|
int val; // an int
val = &myPtr;
|
Line 5 should have been
int** val; // a pointer
this would achieve the ptr to ptr to int you are talking about. And that is what you have in
int **myPtr2 = &myPtr
But my previous answer came from this question:
But I wonder if there's a way to pass the value of a pointer to a regular variable? |
One has to be careful with terminology: A pointer is a variable that holds a memory address,
dereferencing it gives the value of the variable the pointer points to. So, "value of a pointer" is ambiguous - is it the address or the value of the variable?
Since a pointer is a reference then I will need a reference to a reference. |
No, a pointer is not a reference. Even though they may seem to be the same, they are not. Confusion comes from arguments passed by reference (via a ptr) as opposed to passed by value ( a plain variable name). This language comes from C, before references even existed.
So if I wanted to retrieve the value of my second pointer I will have to dereference it like this: (**myPtr2). |
Yes.
the way I interpret reference is basically I'm passing the address which in turns manipulates the object directly. |
No, as per the previous explanations. Look at my code snippet of a reference in my previous post.