Jump to content
  • Advertisement
Sign in to follow this  
Youbar

Confused by Pointers (Beginner's Script):

This topic is 2170 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

// 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 Edited by Youbar

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. Edited by rip-off

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!