## Pointer Operators

Pointers, as you would expect given the not insignificant clue in the name, point to something. Although that’s not necessarily as obviously true as you might think. They point to the memory word at the address specified by the contents of the pointer. This is often referred to as the pointee. That’s all fine and dandy from a conceptual standpoint but how do we get from one to the other in the real world of code?

We have two operators in C: and &. Certainly to begin with the best advice I can give you is to think of as the ‘what’s at’ operator and & as the ‘where is’ operator. You can connect the two thoughts together by thinking of & as the opposite of *. When you get more familiar with these things you’ll want to refer to them by more grown up names, pretty much entirely so you can bill more to your clients; because in fact there’s nothing wrong with the terminology I just gave you.

Terminology aside, if we have the name of a normal, non-pointer variable, we can use the address-of operator & to return the memory address of that variable. Here’s some sample code to illustrate:

You can run the code here if you want to see the output and/or experiment. My output was The address of val is: 0xbfc3ef8c where the 0xbfc3ef8c is in hexadecimal format. Your output address will almost certainly be different. Try checking the output of repeated runs. Do you get the same address each time? What does that tell you about the underlying software and hardware? Is this behaviour likely to be the same on all machines? Try adding more variables and see whether there’s a pattern in their output addresses.

Bear in mind that pointers are just variables too so we can do exactly the same operation on them, even though it might take a bit to wrap your head around:

Here we have &ptr pointing to ptr which contains &val which points to val which contains 1234.

Again, have a play with this code. Try creating pointers to pointers to pointers to pointers for example.

You might have noticed that I just skipped over discussion of the * in that code What’s going on there?

Well, we can also go in the opposite direction to &. If you want the big bucks, this is called dereferencing and is the act of getting the value stored at an address, or in other words, the value in the pointee. We can do this using the * operator, also known as the indirection or dereferencing or value-at-address operator, or in our original terminology, it’s the ‘what’s at’ operator.

One final but important note about pointer operators – you need to read pointer code quite carefully until you get used to it. int ptr says that ptr (the thing pointed to by ptr) is an int. int pointedto = *ptr says pointedto is an int that contains the value pointed to by ptr.

## Memory Electronics

This is just a stub for now but in order to avoid your inevitable disappointment have a lovely link.

From a top level perspective you can think of each bit of computer memory as having the following functionality.

1. It must retain its state (either zero or one) at least semi-permanently. RAM will often lose its state when power is removed and ROM will often lose its state if left for a period of years, but for our purposes here, it remembers what it is set to.
2. It must allow external hardware to read its value.
3. It must allow external hardware to write its value.

## Introduction to Memory

You can think of memory as being like the safety deposit boxes in a bank vault:

Each box is numbered so you can find the right one and each box can hold some contents. In computer memory the number of the ‘box’ is referred to as the address and the contents being stored at that address are referred to as the value.

Here’s a diagrammatic representation of some hypothetical and somewhat simplified computer memory:

Here, we’ve got five memory locations (the rounded rectangles numbered 1 to 5 in the top left corner) each of which is the equivalent of a safety deposit box in our bank vault.

The memory at address 1 (the safety deposit box with the number 1 on the door) contains the value 231 and the memory at address 3 (the safety deposit box with the number 3 on the door) contains the value 34 and likewise for the other memory locations.

Well sure but…

• How come I’m seeing decimal numbers stored at these memory addresses? I thought computers only understood 1s and 0s.
• What are these 0x blah blah addresses I keep seeing?
• How come the addresses I’ve seen elsewhere go up in fours (or eights or some other number) rather than one by one?
• OK But what about the electronics? What’s really going on here?

This is just a stub for now but in order to avoid your inevitable disappointment have a lovely link.

Memory addresses are often written in hexadecimal and will look something like 0x1234abcd. Hexadecimal is a base 16 number system in the same way binary is base 2 and decimal is base ten. Instead of numbers being made up of the binary digits 0 and 1 or the decimal digits 0,1,2,3,4,5,6,7,8 and 9 they are instead made up of the hexadecimal digits 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e and f.

## Grouping of Bits

At the most fundamental level computers are dealing with voltage levels. In reality these voltages vary quite a bit but one of the jobs of the electronics is to ‘condition’ or ‘tidy up’ these voltages to ensure they can be considered either a low voltage or a high voltage. Now, what exactly these voltages are and how low and high are defined varies quite widely between machines but for our purposes just assume that voltages in a computer are low or high.

So, we’ve got two voltage states – low and high. These correspond to what are called bit values, short for binary digit, which are digital 0s and 1s. A low voltage represents a 0 and a high voltage represents a 1. Computer memory stores 0s and 1s. And that’s all it stores.

But it can group these bits together using the conventions of the binary number system into nibbles (4 bits), bytes (generally 8 bits) and words (often 16, 32 or 64 bits – so-called 16, 32 or 64 bit architectures). Why would you want to do that? For two reasons.

Firstly for semantic reasons, in a similar manner to the way we group decimal digits together into numbers. For example the digits 3, 7 and 9 have meaning on their own as individual digits but to express the notion of the number 379 you need to combine the three digits together and treat them as a group.

Secondly, for efficiency reasons. If you only have one wire you can only pass one bit (either a high or low voltage) at any one time (unless you do clever multiplexing things that are not relevant here). It’s quicker to be able to transfer information in parallel which you can do if you have, say, eight individual wires bundled together in a cable (often referred to as a bus in computing) all running in parallel and each carrying its own bit or voltage.

When we’re addressing computer memory we’re usually addressing (or accessing) words rather than individual bits or bytes.