Nicholas Sorrell github twitter

#Beginning Pointers I remember how frustrating it was when our CS 102 class incorporated double pointers. I didn’t even really understand what I was doing with a single pointer (and, looking back now, I’m not so sure UC really primed us for that leap). Most of the class was hopelessly lost, and worse, the assignments built on top of each other. So the guy who tried to use the ‘string’ class was really screwed when we started using those double pointers to point at single ‘char’ pointers (you know, string constants). And, at some point last year, I was reading through a Qt tutorial about how to create a spreadsheet application when it hit me: this is the way to teach pointers!

[Note: While these examples are in C, you can easily translate them to C++ and swap the ‘printf’ statements with equivalent ‘cout’ statements.]

While I realize that I’m the fifty-millionth person to attempt an explanation of this, I do so anyway, if not to waste your time, but my own. So, let’s get to the business of dissolving this subject. First of all, let’s get down to what a pointer is. Simply put, a pointer is just another variable, but instead of having an integer value like ‘5’ or a character value like ‘S’ or a string value like ‘Sorrell’, it stores the value of a memory location. So, to help our compiler along, we tell it which type of variable our memory location is pointing to by giving our pointer a type (like ‘int *’, or ‘char *’).

One question that was never, ever, raised or addressed was this: Why would we ever want to use pointers?

I’m sure that if you look on the interwebs, you can find a million reasons. But here are a few useful ones.

##Cut To The Code So, remember hours ago when I mentioned that spreadsheet application? And how it pertained to learning pointers? Well, here’s what I was talking about. Suppose we have the code below:

int main()
{
    int i = 5;
    int * iptr = &i;    // that says the pointer 'iptr' is equal to the address of 'i'
    return 0;
}

We have an int-type variable (i) with a value of 5. And we have an int-type pointer (iptr) with a value of address-of-i. Now, to visualize this, let’s open up our spreadsheet. And let’s become the computer and do the memory management ourselves (I know, you can barely contain your excitement). So, let’s assign cell A1 to the variable ‘i’, and put a value in that cell.
A1

Now, see that ‘fx’ area that is highlighted? That tells us the actual value of that cell. And in this case, it’s 5. Let’s now assign cell A2 to the variable ‘iptr’, and put a value in that cell.
A2

See the value that we’ve put into A2 (up by that little ‘fx’ area)? We’ve given it the value ‘=A1’. So cell A2 effectively points to A1. Now, when we change A1, A2 appears to change with it! Another way of saying this is that A2 references A1 - and when we dereference A2, we get the value of A1. Our spreadsheet automatically dereferences for us and puts that value in the cell (even though ‘fx’ value never changes).

So, with all this talk of dereferencing, what does that look like in the code?

int main()
{
    int i = 5;
    int * iptr = &i;
    *iptr = 42;     // this is the 'dereferencing' we're talking about
    printf("i's value is %i \n", i);       // this prints 'i's value is 42'
    i = 5;
    printf("i's value is %i \n", *iptr);       // this prints 'i's value is 5'
    return 0;
}

As you can see, the ‘*’ character dereferences. So let’s recap:

See? Pointers are that easy! Here’s a couple of parting tips before the next blog post.

In my next post, we’ll talk about double pointers and arrays.

blog comments powered by Disqus