passing the value of a pointer to a regular variable

I have the following code snippet:

1
2
3
4
5
6
7
8
9
10
 int main (){
    int tmp = 5;
    int *myPtr = &tmp;  // Assign the address of tmp to our pointer.  They now have the same address in memory
    
    int val;
    
    val = &myPtr;   // This is saying that we want to copy the value of myPtr (since we dereferenced it). Not the same address in memory!
    // & is known as the reference operator
}


Compiler is throwing me the following error:
Assigning to 'int' from incompatible type 'int**'

Which makes sense to me. But I wonder if there's a way to pass the value of a pointer to a regular variable?
Hi,

on Line 7 the & is taking the address of an existing pointer, hence int**. & is not a reference in this situation.

A reference looks like this:

void MyFunction(const std::string& MyParameter); // MyParameter is a reference to a const string

 
val = *myPtr;  // val is now 5 


The * here deferences the pointer, giving the value it points to.

It's a little tricky, the operators have different meaning in different contexts.

Last edited on
Thanks TheIdeasMan,

I think I figure it out and correct me if I'm wrong. & in any situation points to an address in memory, whether it is from a regular variable or a pointer. Line 3 in the code means assign the address of tmp//regular variable to a pointer to myPtr. 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. Since a pointer is a reference then I will need a reference to a reference. In this case int **myPtr2 = &myPtr. So if I wanted to retrieve the value of my second pointer I will have to dereference it like this: (**myPtr2). Not sure if my explanation makes sense? I think the key here is that myPtr is a pointer to an existing address already and the way I interpret reference is basically I'm passing the address which in turns manipulates the object directly.
Last edited on
& 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.
Last edited on
Very Enlightening. Thanks!
Topic archived. No new replies allowed.