Pointers are an important part of C. If you don’t understand how pointers work, you use alot of the power and flexibility of C. Pointers are difficult to understand if you don’t try.
We define a pointer like so:
int *p;
The * shows that this isn’t a variable, it’s a pointer. A pointer does what it says, it points. Also, the pointer must be a certain type, as with variables. if it’s going to point to an integer, we define it as an integer pointer. Let’s have another look:
int num, *p; num = 10; p = #
Remember the ’&’ sign from the last chapter? What this sign means is, instead of assigning the “value” of num to the pointer, we actually assign the address in memory where the data for num is stored. Let’s say that again, p will contain the address in memory of num.
Now one thing about pointers is that, logically, they contain two values.
- p is the location holding the address in memory of
num. - *p is the location pointed to by that address.
So if we use p we can manipulate WHERE in memory the pointer is pointing to. Yet if we use *p we can manipulate the data contained within the address that the pointer is poining to. If we alter *p, we also alter the variable it points to, let’s look at an example. p contains the pointed-to address, *p is another name for the variable it is pointing to. Get it? I hope so, it’s kind of confusing at first.
#include <stdio.h>
int main()
{
int num, *p; /* define variable and pointer */
num = 7; /* number now equal to 7 */
p = # /* p contains ADDRESS IN MEMORY of num */
*p = 5; /* num now equals 5 */
num = 3; /* num AND *p now equal 3 */
}
Think of *p as an alias for the variable it points to, it allows you to manipulate the data within that variable. While p controls which variable, which address in memory we are pointing to. Pointers allow us to jump around memory addresses and manipulate the data in them, allowing us to get into a lower-level, into the workings of things. To fully understand we should discuss memory addresses.
Memory addresses
All computers have memory, also called RAM. RAM holds the programs your computer is currently running, along with the data the programs are currently manipulating. Each byte of memory has it’s own address, the address of the first byte is 0, followed by 1, 2, 3… etc. Memory can group bytes together if it needs to form a larger variable, this happens when we declare an int variable.
An int variable is assigned four consecutive bytes in memory. When the program runs, the computer reserves space for that variable somewhere in memory, and from here on the data in the variable can be accessed by the computer by the address at which it is located.
Consider the following:
int number; number = 10;
| Address | Data |
| 220, 123 | - |
| 220, 124 | - |
| 220, 125 | - |
| 220, 126 | 10 |
| 220, 127 | |
| 220, 128 | |
| 220, 129 |
Do you see? Now the computer knows that the data for number is stored in four consecutive memory addresses starting at 220, 126. Now if we declare a pointer and do:
int *pointer = &number;
Memory could now look something like this:
| Address | Data |
| 220, 123 | - |
| 220, 124 | - |
| 220, 125 | - |
| 220, 126 (number) | 10 |
| 220, 127 (number) | |
| 220, 128 (number) | |
| 220, 129 (number) | |
| 220, 130 | - |
| 220, 131 | - |
| 220, 132 | - |
| 220, 133 (pointer) | 220, 126 |
| 220, 134 (pointer) | |
| 220, 135 (pointer) | |
| 220, 136 (pointer) |
The value we assigned to number is stored in its area in memory, and the pointer also has it’s own place in memory. The thing about the pointer is that the data it contains is the address in memory of the variable to which is “points”. If we change the value stored in the pointer, we change where it points. One thing i must mention, most computers today use 32-bit (4 byte) memory addresses. So the pointer is assigned 4 bytes of memory. Some computers use 64-bit memory addressing, meaning pointers on that computer would take up 8 bytes in memory.
But, why use them?
I asked the same question, it seems pointless, and in the above examples i suppose it is. I’m not showing you why pointers are used, i’m trying to explain how they’re used, and to help you understand how they work. The advantages are pretty unclear, but as you progress and learn more about different data types you will realise the advantages. You can quickly change something with a pointer rather than changing much more data. It’s just an easy way to work at the memory address level, manipulate data contained there, it allows you to manipulate data in memory more accurately. Make good use of pointers, they make C programs much more efficient.