does anyone know what the difference is between:
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using std::cout;
- using std::endl;
- void func( int *p1, int* p2 )
- {
- p2 = p1;
- }
- int main(int argc, char* argv[]) {
- int* p2;
- int* p1 = new int;
- func(p1, p2);
- cout << "P1 Address: " << p1 << " P1 Value: " << *p1 << endl;
- cout << "P2 Address: " << p2 << " P2 Value: " << *p2 << endl;
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using std::cout;
- using std::endl;
- void func( int *p1, int* p2 )
- {
- p2 = p1;
- }
- int main(int argc, char* argv[]) {
- int* p2;
- int* p1 = new int;
- p2 = p1;
- cout << "P1 Address: " << p1 << " P1 Value: " << *p1 << endl;
- cout << "P2 Address: " << p2 << " P2 Value: " << *p2 << endl;
- return 0;
- }
output first:
P1 Address: 0x804b008 P1 Value: 0
P2 Address: 0x804b008 P2 Value: 0
output second:
P1 Address: 0x804b008 P1 Value: 0
P2 Address: 0xbfb680f8 P2 Value: -1078558376
Obviously its not the same address and therefore not the same value, but can someone explain why?
Thanks,
amonsch
P.S.
I use g++ (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291]
with openSuse 11.1 on a 32-bit maschine