Milktrader

Iterating Until Convergence

Thursday, December 2, 2010

A Neighborhood Metaphor for C Pointers

I take back all the mean things I've said in the past about C pointers. It was really more a product of confusion, but all that has changed now. I am now pleased to present an explanation of C pointer basics in terms of a robust metaphor.

Let us assume that we are all comfortable with the notion that in C, a variable is created with an explicit data type. When you create a variable, you are creating a rental house. In C, there are strict rules about who gets to live in what house. It's just the way it is.


First, let's create an empty rental house for an integer household. And further, let us call this rental house 'gold'.


int gold;

There, that was easy.

Now, let's approve a rental agreement between the integer 1400 and the rental house whose name is 'gold'.


gold = 1400;

As master planners of this neighborhood, we may find it convenient to create a map room where we can store the address of a specific rental property. Map rooms need to be specific to each neighborhood, so let us first create a map room that will at some point contain a map to a rental property in the integer neighborhood.

Now comes the confusing part. In C, the * operator means different things in different contexts. This is a bit discouraging, but not everything is perfect in programming so we must learn to adapt to this issue.  For our purposes, we will ignore the intuitive meaning of * as multiplication operator and focus on two uses it has in the world of map rooms. Creation of map rooms, and the empowering of a map room (or the star deputizing) to evict current rental housing residents and replace them with other residents.

First, we need to create the map room. We do it thusly:


int *feif;

The clue that this is a map room creation process is that 'int' -- or our data type of choice -- precedes the * operator. In any case, we have, with that simple line of code, created a map room for an address of a rental property in the integer neighborhood. It's an empty map room, but a map room that we can be proud of nonetheless. 

The map room is pretty small, being just a room and all. So if we try to stuff a family in the room, it won't work.


feif = 1500;

Our map room named feif cannot board an integer family, but it can store the address of a rental house in the integer family. So how do we put an address into the map room? Ah, the & operator of course. This operator is commonly translated as 'the address of' and makes sense when you place a variable after the operator.


&gold

is literally 'the address of' the rental property called 'gold' in the integer neighborhood. Now, let's put the address into our freshly minted map room named 'feif'.


feif = &gold;

There, feif is now happy. He has a map. Now he is ready for some work. As managers of this integer neighborhood, say for a moment that we've decided that 1400 should not be allowed to live in the rental property named 'gold' any longer. But instead of going to the house ourselves and over-writing the last assignment, we want feif to do the dirty work for us. We first must give him a modicum of authority, and we shall with the * operator. In this next line, we hereby deputize feif, and give him instruction to go to the address of the gold rental property in the integer neighborhood, knock on the door, ask whoever is in there now (we know it's 1400) to please leave because he has new occupants ready to move in.


*feif = 1500;

Now, if we ask who is living in the rental property named 'gold' in the integer family neighborhood, we will be pleased to discover that it is 1500, our preferred occupants.

Thanks deputy feif, you did a great job!

0 comments:

Post a Comment