Very confused between pointers and references

shouldnt i get the adress for v[2] since its vptr2 is pointing to it?

1
2
3
4
5
6
7
8
 int main(){
int v[5] ={0, 10, 20 ,30, 40};
int *vPtr2;
vPtr2 = &v[ 2 ];
cout << &vPtr2<< endl; // what is the difference between this statement
cout << vPtr2<< endl; // AND this statement? i get different outputs  

return 0; 
Last edited on
vPtr2 is a variable. Like any other variable, it has an address (the memory location where it is stored). That's what's printed at line 5.

The contents of vPtr2 is a pointer to an integer. At line 4 you assign the contents to the address of v[2]. That's what you print at line 6.
To understand this, first ignore the actual type that v or vPtr2 is.

Just look at this, out of context. foo is a variable, with an unknown type:
When you print cout << foo << endl; you are printing the value of foo.
When you print cout << &foo << endl; you are printing the address of foo.

The value of foo might be "hello", or 42, or something else.
The address of foo is where that variable itself is being stored in the memory of the computer.


A pointer variable is just another type of variable -- it will have a value, and address. Only in this case, the value of a pointer variable is the address of another variable or location in memory.
The pointer variable itself still has an address, which is where the value of the variable is stored in memory.

Every variable you declare has to be stored in memory somewhere, regardless of its type.

PS: Nothing in your code shows a reference. &var is just the addressing operator, which is a separate concept from the C++ feature of references.
Last edited on
think of a reference as an alias or nickname. Its the same guy, just going by a different name.

think of a pointer as an array index into the great big array in the sky that we call 'memory'.

you don't have a reference. this is a reference:

1
2
3
4
int  joebob = 13;
int &joe = joebob; //IMPORTANT: THE & IS ON THE NEW VARIABLE NOT THE OLD.  
joe = 10;
cout << joebob << endl;


what you have is the address of a pointer, which is a 2-d array or a ** or similar ideas.
you said
int * ip = new int;
do something with &ip; //see the & on existing variable?
which is the address of the address of the integer pointed to by ip.
or maybe more clearly,
the location in memory of the variable that stores the location in memory of the integer.


That said, pointers and references are very similar in some ways. There is some overlap in behavior ... both of them can, with the right syntax, modify another variable. That is, a pointer can be used to approximate a reference. You will just need some practice to understand this, but if you think about it, a reference is the same object, a pointer can point to the other object, so they can both access the other object as if they were it.
Last edited on
BTW, note that there are no references in the OP. The & in that code is the "address of" operator.
Topic archived. No new replies allowed.