Skip over navigation

What Are Pointers?

Pointers Point

Terms

Problems

What is a pointer exactly?

Throughout an introduction to computer science class, you may hear references made to wonderful things called pointers, but until you experience them for yourself, you're missing out on a wonderful world of possibilities. Pointers are a necessary facet of almost every computer program written, whether for personal, academic, or commercial use. This tutorial will explain when and how to use them.

What exactly is a pointer? Stated simply, a pointer is nothing more than a variable that holds an address in the computer's memory. This is where a pointer gets its name. A pointer variable holds the address of a certain piece of memory in the computer; in other words, a pointer points at a specific location in memory. In essence a pointer is nothing more than a variable that holds a number that is the address of a specific memory location.

Pointers and Memory

For now, let's think of memory as a big blob of storage space where we can put information that we later want to retrieve (this isn't far from the truth).

Figure %: The memory in our computer

Let's say we have a simple program as follows:


void main()
{
	int steve;
	steve = 220;
}

What happens when we run this program? First, the computer sets aside a little bit of memory to hold the integer steve.

Figure %: Memory set aside for steve

It then stores the value 220 into that variable.

Figure %: Storing the value 220 into steve

This seems straightforward, and it is. But behind the scenes, there is more going on. As coders, we are able to access the variable steve just by using its name. But how does the computer know where into memory to put the value we store into steve? The answer is that every variable stored in memory has an address associated with it, and the computer keeps track of these addresses. When you tell it to store a value into the variable steve, the computer finds the address at which steve is located, and puts the value into the memory at that location.

Memory Addresses

What does the concept of "addresses" really mean in terms of a computer? What it means is that every piece of the computer's memory is numbered so it can be found easily. A better pictorial representation for memory, as opposed to the blob above is a straight segment of memory, as follows:

Figure %: A pictorial representation of memory

In this figure, each box represents one byte of memory. What are the numbers below each box? Those are addresses. Each number corresponds to one byte; in other words, we can find and access any byte in memory just by knowing its address (remember that a byte is 8 bits. A bit is the smallest unit of storage in a computer, storing either a 0 or a 1).

Let's return to the example from above, and let's say that steve was stored at address 728 in memory and the value 220 was stored into steve:

Figure %: Storing 220 into memory address 728

This figure raises a few questions.

First, why does steve cover bytes 728, 729, 730, and 731? I thought we were just storing it into 728? Not exactly.

Remember that steve is an integer and on most modern computers an integer is a 4-byte data type, meaning that one integer takes 4-bytes, or 32 bits, to be stored. When we say that the address of steve is 728, what we mean is that steve starts at 728 and continues linearly through the memory for as many bytes as needed. Had steve been a character, which on most computers is a single byte data type, steve would have been stored entirely in memory address 728.

Second, what is this "011011100" thing? It's binary notation. When humans do arithmetic, we often use base 10, meaning that each digit in a number represents some power of 10. For example, the decimal number 220 means 2*102 +2*101 +0*100 = 220 . But there is no reason we have to use base 10; we can use any base we like. For computers, base 2 is the easiest. In Base 10, we can use digits 0 through 9; in base 2 we can only use the digits 0 and 1. Why is this the easiest base for computers? Because two numbers, 0 and 1, are easily represented by the two states of a simple switch, on and off. Inside your computer are hundreds of millions of these tiny switches that can either be on or off, representing a 0 or a 1. This corresponds nicely to base 2 notation. When you store a number in a computer, the computer actually stores it in base 2, even though you may have entered it in base 10. So, when we store the decimal number 220 into the computer, it is stored in base 2: 1*27 +1*26 +0*25 +1*24 +1*23 +1*22 +0*21 +0*20 = 220 , hence the "011011100".

Another base commonly used by computer scientists is hexadecimal notation. Hexadecimal is Base 16, meaning that each digit represents 16 raised to a power (as opposed to 10 raised to a power in decimal notation, or 2 raised to a power in binary notation). The digits in hexadecimal are represented by the numbers 0 through 9, and then the letters A through F, where A is 10, B is 11, etc, through F, which is 15. Why hexadecimal? Because 16 is a power of 2 and corresponds nicely to binary. Every hexadecimal digit (a hexit) is equivalent to four binary digits. Because of this, it is easy to convert from hex to binary and vice versa. This easy conversion makes hexadecimal a convenient notation for representing binary numbers in a more compact form. To let us know that a number is hexadecimal, it is preceded by a "0x". For example, the decimal number 220 is equivalent to the hexadecimal number 0xDC: D*161 + C*160 = 13*16 + 12 = 220 .

Octal notation, base 8, is also a common base used by computer scientists for a reason similar to that of hex: 8 is a power of 2. A single octal digit (an octit) is equivalent to three binary digits. Octal notation places a 0 in front of every number.

Base Representation
Base 10 (decimal) 220
Base 2 (binary) 0b011011100
Base 8 (octal) 0334
Base 16 (hexadecimal) 0xDC

For more information on number representation and bits, please refer to the SparkNote on the topic.

So what?

Back to the topic of pointers. Just as the purpose of the steve variable is to store an integer, the purpose of a pointer variable is to store a memory address, often the address of another variable, such as steve. In the next section, we'll see how to declare pointers and how to use them. And after that, we'll see the answer to the question that is probably forefront in your mind: "why?"

Follow Us