Home Page

Learning C++ Pointers for REAL Dummies

Introduction

Beam me up Scotty, multiple times!!!

The question might come up, "How can I get a pointer to point to a pointer?" Ok, maybe this question won't come up in your mind, but it might come up in your programming. I just want to touch on the subject on how to do it.

First let's look at some code:

int **ramon;
int *paul;
int melissa = 5;
paul = &melissa;

ramon = &paul;
printf("ramon = %d\n", ramon);
printf("&paul = %d\n", &paul);
printf("*ramon = %d\n", *ramon);
printf("&melissa = %d\n", &melissa);
printf("**ramon = %d\n", **ramon);

Let's take this line by line. The first line declares a double pointer called ramon. We then declare a pointer called paul. And finally we declare a regular integer called melissa which we initialize to 5.

Next, we point paul to melissa, which stores melissa's address into paul's house. Then we point ramon to paul, which will store paul's address into ramon's house.

So, now let's look at what will be printed out using the diagram below's addresses.

ramon = 1000
&paul = 1000
*ramon = 500
&melissa = 500
**ramon = 5

Well the first printout is pretty simple. What is stored in ramon's house which is the address of paul's house. We check that with the second printout. Then we printout the value of what ramon is pointing to. This would be melissa's address since that is what paul is storing. We check this with the fourth line of code. Now, the final line is the tricky part. It simply asks, "Whatever is the value that ramon is pointing to, take that value and see what is the value that it is pointing to." If that made no sense at all, another way to think of it is breaking it up. For example:

*(*ramon)

Take the value of what ramon is pointing to, 500. Now take 500 and see what is stored in that address, 5. See, it is that easy. Ok, well, maybe the picture below will help you out.

The best way to learn this stuff is just doing some programming examples on your own and see what comes out.

Hope you had fun learning this material as I have had making it. Make sure you take the QUIZ for REAL Dummies to see if you learned anything. Take care.

 

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