pointer question

Hello, i was just starting to understand pointers until I came across the code below(from tutorial on this website). I fully understand it up until the line in bold. It outputs 10 for 'firstvalue' and 20 for 'secondvalue'. I just don't get how the 'secondvalue' has the value of 20 at the end??

// 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;
}
That bold line stores 20 in the address to which it points, which is the same as the address p2 points to (due to the line above that one) which is the same as &secondvalue (due to the line p2 = &secondvalue). See?

-Albatross
Last edited on
Hello Mike200,

This is my first post to the forum -- I hope my explanation is clear enough.

A pointer is a value that represents a location in memory. In the application above you have two values ( firstvalue and secondvalue). And you have two pointers (p1 and p2).

p1 = &firstvalue;
The & in front of the value means that you want the address of that value. P1 is then assigned that address.

*p2 = *p1
When you declared p1 and p2 you used the * symbol to declare that they where pointers. When you use the * symbol after you declare the pointer you are dereferencing the pointer -- you are saying that you don't want the value of the pointer, you want the value the pointer points at.

To answer your question, before your bold face code, your teacher assigned p2 to p1. Essentially, p1 was pointing at secondvalue. Then he set the value at that address to 20.


Joseph has a pretty solid explanation, but I'll just throw in my two cents.

p1 = &firstvalue

Whenever you access the value of p1, it's not going to give you a static value, it's going to "redirect" you to whatever value firstvalue is holding. The & operator is saying "goto the address of".

*p1 = 10

The dereference operator (denoted by an asterisk, "*") essentially means "We took the detour and are now at the location of where we were assigned to redirect to (the address of "firstvalue"). Since you are "at the location in memory", we are allowed to do whatever we want. "*p1 = " is the same thing as "firstvalue = ".

So, since we know that, then:

1
2
3
4
5
6
7
8
p1 = &firstvalue;
firstvalue = 10;
cout << firstvalue << endl; // Prints 10
cout << *p1 << endl; // Prints 10
firstvalue = 20;
cout << *p1 << endl; // Prints 20
*p1 = 30;
cout << firstvalue << endl; // Prints 30 


In conclusion, you can remember both of the pointer operators as such:

& means get the location of wherever a variable is, and when assigned to a pointer, means that the pointer redirects to the location of whatever variable you assigned it to.

* means follow the path that the redirect takes you to and gets the value of wherever it leads you to. If the pointer is assigned to memory, then we go to that memory location and see what value is stored.

------

Redundant, I know, but it hammers it into your head. Pointers can be complicated but a lot of people create little analogies to help them remember (I'll be honest, writing this even helped my understanding of pointers!)
Thanks for all your replies. Thinking about it ..the line thats causing me problems was the line above the bold line.I've stepped through the code in my debugger and watched the values change line by line but still I'm confused!!. The line p1=p2, what is happening here exactly? if p1 address is changed after thi point, it no longer points to firstvalue?Isn't this link now broken p1 doesn't equal &firstvalue any more?with p1 now essentially pointing at the second value I was expecting the firstvalue to some how cut adrift?

Please excuse my lack of knowledge..
Unlike non-pointers, accessing a pointer (i.e. without any operators) returns it's address. So, knowing that, when we say "p2" by itself, it returns the address of it. Pointers don't need the "&" operator.

p1 = p2

Assign the address that p1 redirects to, to the address of p2.

When you access the value of "p1", this is what happens:

p1 ---- Redirecting to address of p2 ----> p2 ---- Redirecting to address p2 is assigned to ----> Destination

If it helps, think of detours on a street. That's all a pointer is.
Topic archived. No new replies allowed.