Thank you , can I ask you something , when I try to show the location in the memory for pointers I'll use this cout<<&ptr;
but if I used this cout<<*&ptr;
it shows another location , what's the difference ?
A pointer is a variable which contains the address of another variable.
cout << &ptr; will print the address of the 'ptr' variable.
cout << *&ptr; is the same as cout << ptr; which prints the contents of 'ptr'... ie, it prints whatever address is stored inside the ptr variable.
Example:
1 2 3 4 5 6 7 8
int x = 5;
int* ptr = &x; // ptr points to x
cout << &x; // prints the address of x
cout << ptr; // prints the contents of ptr -- which prints the address of x (same as above line)
cout << *&ptr; // same as above lines
cout << &ptr; // prints the address of 'ptr', which will be different from address of x
// since they are 2 separate vars.
Passing something by reference can be seen as passing the address of something.
Passing a pointer by reference would pass the address of the pointer storing the address of an object. So don't do it unless the function is there to change what the pointer is pointing.