Home Page

Learning C++ Pointers for REAL Dummies


What you got in your house? (*)

Previously in Where do you live?, we set up an example where we put paul into a house located at 1500 Computer Lane storing the value 21. Then, we put melissa into a house storing the address of paul's house, 1500.

Now, what if we wanted to know what was the value stored at the house in which melissa was pointing at. To do this we would use the reference operator (*). This could be thought of as saying, "value pointed by". For example:

dave = *melissa;

This will store 21 in dave's house. To follow our simile, dave will ask melissa what value she is storing. She will say, "1500." Dave will then go to address 1500 and ask the person living there what is stored in their house. That person, who we know is paul, will say, "21." So, dave will know to store 21 in his house. Graphically it would look something like this:

For a Flash version, click here.

So from this example, if we printed out the value of dave, it would print out 21. We could also type in:

*melissa = 30;

This would read, "The value pointed by melissa equals 30." So, paul will equal 30, but dave will not equal 30 since he does not get updated. He stores what he originally stored and does not change.

Until now, I have glossed over the detail on how melissa is created. The next section explains how to declare a pointer of a certain type.

Where do you live? (&)
What you got in your house? (*)
Don't point, it's rude!
I am pointing here!
Where are you pointing?
How do I point to the neighbor?
You guys are brothers?
Beam me up Scotty, multiple times!!!
Quiz for REAL Dummies