Confused by Pointers (Beginner's Script):

Started by
5 comments, last by rip-off 11 years, 4 months ago
// more pointers
#include <iostream>
using namespace std;
int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20

cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}


I understand all but this line of the script, which I don't see any reason for:
p1 = p2; // p1 = p2 (value of pointer is copied)

I can see that it says it was copied, but I thought that already happened in the line above that?
*p2 = *p1; // value pointed by p2 = value pointed by p1
Advertisement
How does this not have any views at all?
What is it you find confusing about the two lines? One assigns pointers, the other assigns the values pointed to by the pointers. The line p1=p2 simply means that the two pointers point to the same address, while *p2=*p1 assigns the values pointed to by the two pointers.
Oh, I see now. Thanks for clearing that up for me.
If there's no views it just means that nobody has looked yet.

Traffic varies.

Breathe deeply. tongue.png
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
A pointer contains the address of something, in the line above the asterisk "dereferences" the pointer (meaning it goes to the location the pointer is pointing to).
The first line says "pointer1 now points to the same thing as pointer2",
the second line says "the thing that pointer1 is pointing to is now the same as the thing that pointer2 is pointing to".
* and & can be a bit confusing for beginners because sometimes it depends on the context. For me it helps to put it into a phrase in this manner.
The purpose might become clearer when additional output is used:

// more pointers

#include <string>
#include <iostream>


using namespace std;

void print(string description, int value1, int value2, int *pointer1, int *pointer2) {
cout << description << ": " << endl;
cout << " first value is " << value1 << endl;
cout << " second value is " << value2 << endl;
cout << " first pointer is at " << pointer1 << " with value " << *pointer1 << endl;
cout << " second pointer is at " << pointer2 << " with value " << *pointer2 << endl;
cout << endl;
}


int main()
{
int firstvalue = 5;
int secondvalue = 15;

int * p1 = &firstvalue;
int * p2 = &secondvalue;

print("Initial values", firstvalue, secondvalue, p1, p2);


// value pointed by p1 = 10
*p1 = 10;
print("After setting *p1 to 10", firstvalue, secondvalue, p1, p2);

// value pointed by p2 = value pointed by p1
*p2 = *p1;
print("After settings *p2 to *p1", firstvalue, secondvalue, p1, p2);

// p1 = p2 (value of pointer is copied)
p1 = p2;
print("After pointing p1 to p2", firstvalue, secondvalue, p1, p2);

// value pointed by p1 = 20
*p1 = 20;
print("After setting *p1 to 20", firstvalue, secondvalue, p1, p2);

return 0;
}


On my system, this produces:

Initial values:
first value is 5
second value is 15
first pointer is at 0x7fffe8171510 with value 5
second pointer is at 0x7fffe8171514 with value 15


After setting *p1 to 10:
first value is 10
second value is 15
first pointer is at 0x7fffe8171510 with value 10
second pointer is at 0x7fffe8171514 with value 15

After settings *p2 to *p1:
first value is 10
second value is 10
first pointer is at 0x7fffe8171510 with value 10
second pointer is at 0x7fffe8171514 with value 10


After pointing p1 to p2:
first value is 10
second value is 10
first pointer is at 0x7fffe8171514 with value 10
second pointer is at 0x7fffe8171514 with value 10

After setting *p1 to 20:
first value is 10
second value is 20
first pointer is at 0x7fffe8171514 with value 20
second pointer is at 0x7fffe8171514 with value 20


As you can see, after that line the two pointers point at the same location, and dereferencing them yields the same value.

This topic is closed to new replies.

Advertisement