tags:

views:

103

answers:

5

I have a sample code :

#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
  int firstvalue = 5, secondvalue = 15;
  int * p1, * p2;

  p1 = &firstvalue;  
  p2 = &secondvalue; 
  cout << "1.p1: " << p1 << ", p2: " << p2 << endl;
  cout << "1.*p1: " << *p1 << ", *p2: " << *p2 << endl;
  *p1 = 10;
  cout << "2.p1: " << p1 << ", p2: " << p2 << endl;
  cout << "2.*p1: " << *p1 << ", *p2: " << *p2 << endl;
  *p2 = *p1; 
  cout << "3.p1: " << p1 << ", p2: " << p2 << endl;
  cout << "3.*p1: " << *p1 << ", *p2: " << *p2 << endl;
  p1 = p2;
  cout << "4.p1: " << p1 << ", p2: " << p2 << endl;
  cout << "4.*p1: " << *p1 << ", *p2: " << *p2 << endl;
  *p1 = 20;          
  cout << "5.p1: " << p1 << ", p2: " << p2 << endl;
  cout << "5.*p1: " << *p1 << ", *p2: " << *p2 << endl;  
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  cout << "firstvalue is " << &firstvalue << endl;
  cout << "secondvalue is " << &secondvalue << endl;

  getch();
    return 0;
}

And here's the output :

1.p1: 0041FB40, p2: 0041FB34
1.*p1: 5, *p2: 15
2.p1: 0041FB40, p2: 0041FB34
2.*p1: 10, *p2: 15
3.p1: 0041FB40, p2: 0041FB34
3.*p1: 10, *p2: 10
4.p1: 0041FB34, p2: 0041FB34
4.*p1: 10, *p2: 10
5.p1: 0041FB34, p2: 0041FB34
5.*p1: 20, *p2: 20
firstvalue is 10
secondvalue is 20
firstvalue is 0041FB40
secondvalue is 0041FB34

What is copied in the line "p1 = p2" ? Does p1 become reference to p2 or does it work in different way ?

+1  A: 

memory address p1 takes on the memory address of p2 (0041FB34)

sjchoi
so no reference magic there ? thanks :)
terence6
apart from the use of 'cout' and '<<', your sample is plain C. and C, almost by definition, doesn't to magic.
Javier
+2  A: 

You just say: "Now p1 should point at the same memory block as p2 does".

So they start pointing at the same memory block (to the same address) and, hence, share the same values.

Kotti
A: 

After the assignment, p1 will contain the same memory address as p2. The two pointers will point to the same variable, so *p1 and *p2 refer to the same memory.

Aside of that there is no special connection between p1 and p2. For example a following assignment like p1 = &firstvalue will not change anything about the p2 pointer variable and p1 and p2 will point to different memory locations again.

sth
+1  A: 

Since p2 contains the address of secondvalue, p1=p2; copies the address of secondvalue from p2 into p1. I'd advise against calling it a reference if you can help it though -- a reference is something else that's similar enough to a pointer to cause confusion anyway, so calling a pointer a reference (or even thinking of it as a reference) is probably a poor idea.

Jerry Coffin
A: 

= (the assignment operator) just copies the values of one variable (p2) into another (p1). So, after the assignment, p1 and p2 are synonymous in that they reference the same address -- dereferencing them will give the same value, as seen in your printout.

For more information, try this article on pointers.

btlachance