A first look at pointers
Addresses, and how a function reaches a variable that belongs to its caller.
Memory is a row of numbered boxes
Every variable lives somewhere in the chip's memory. Picture memory as a very long row of boxes, each holding one byte and each with a number. That number is the box's address.
When you declare int count = 5; the compiler picks some boxes for count and puts 5 in them. Normally you never need to know which boxes. Sometimes it is exactly what you need.
A pointer is a variable that holds an address. That is all it is. Instead of holding a temperature or a count, it holds the location of another variable.
Two new operators
| Operator | Read it as | Meaning |
|---|---|---|
&x | "address of x" | Where x lives |
*p | "the value p points to" | The variable at the address stored in p |
#include <stdio.h>
int main(void) {
int count = 5;
int *p = &count; // p holds the address of count
printf("count is %d\n", count);
printf("count lives at %p\n", (void *)p);
printf("the value at that address is %d\n", *p);
*p = 9; // store 9 at that address
printf("count is now %d\n", count);
return 0;
}
Read the declaration int *p as "p is a pointer to an int". After p = &count, the names *p and count refer to the same boxes. Changing one changes the other, because there is only one variable.
| Name | Holds | Example value |
|---|---|---|
count | a number | 9 |
p | the address of count | 0x2000FFE8 |
*p | whatever is at that address | 9 |
The address printed is in RAM, which starts at 0x20000000 on this simulated chip. You never choose these numbers yourself.
What pointers are for
In the Functions guide you saw that a function receives copies of its arguments and cannot change the caller's variables. Pointers are the way round that. Give the function the address of a variable, and it can reach the original.
#include <stdio.h>
void set_to_zero_wrong(int value) {
value = 0; // changes a copy
}
void set_to_zero(int *value) {
*value = 0; // changes the variable at that address
}
int main(void) {
int a = 7, b = 7;
set_to_zero_wrong(a);
set_to_zero(&b); // pass the address of b
printf("a = %d, b = %d\n", a, b);
return 0;
}
The second function has int * as its parameter type, and the caller passes &b. Those two go together. If a function wants a pointer, you give it an address.
Returning more than one result
return hands back a single value. A function that needs to produce two takes the addresses of two variables and fills them in. Parameters used like this are called output parameters.
#include <stdio.h>
void divide(int total, int parts, int *quotient, int *remainder) {
*quotient = total / parts;
*remainder = total % parts;
}
int main(void) {
int q = 0, r = 0;
divide(17, 5, &q, &r);
printf("17 divided by 5 is %d remainder %d\n", q, r);
return 0;
}
The pointer that points to nothing
A pointer can hold the special value NULL, meaning "this does not point at anything". Functions return it to say "not found", and careful code sets unused pointers to it.
Following a NULL pointer is an error. On a microcontroller the program stops dead. Run this to see what the simulator reports.
#include <stdio.h>
int main(void) {
int *p = NULL;
printf("%d\n", *p); // there is nothing at this address
return 0;
}
So a function that is handed a pointer checks it first.
#include <stdio.h>
int value_or(const int *p, int fallback) {
if (p == NULL) {
return fallback;
}
return *p;
}
int main(void) {
int reading = 42;
printf("%d\n", value_or(&reading, -1));
printf("%d\n", value_or(NULL, -1));
return 0;
}
The word const in const int *p promises that the function only reads through the pointer and does not change the variable.
Arrays were pointers all along
In the last guide you saw that an array is not copied when it is passed to a function. Here is why. Passing an array really passes the address of its first element. The function receives a pointer.
That is also why a function cannot know how long an array is. It has been told where the array starts and nothing more.
#include <stdio.h>
int first_element(const int *values) {
return *values; // the value at the start of the array
}
int main(void) {
int data[3] = { 10, 20, 30 };
int *p = data; // the array's name gives the address of data[0]
printf("%d %d\n", first_element(data), *p);
printf("%d\n", p[2]); // a pointer can be indexed like an array
return 0;
}
&data[1] is the address of one particular element, so a function can also hand back a pointer into an array.
One trap to remember
The * and ++ operators interact in a way that catches everyone once.
| Expression | What it does |
|---|---|
(*p)++ | Adds one to the value p points to |
*p++ | Moves the pointer p along, and leaves the value alone |
When you mean "increase the value", write the parentheses, or write *p += 1.
Where this leads
The hardware itself is reached through pointers. A peripheral's control register sits at a fixed address, and firmware switches a pin by writing through a pointer to that address. Before that, the next lesson shows how to group related values into one struct, and how a pointer reaches inside it.
Common mistakes
- Using a pointer that was never given an address. Set it to something real, or to
NULL. - Following a
NULLpointer without checking it. - Forgetting the
&when calling a function that wants an address. - Forgetting the
*when you want the value rather than the address. - Writing
*p++when you meant(*p)++.
Interview question: Why does C need pointers at all?
Answer: For three reasons. Functions receive copies of their arguments, so a pointer is the only way for a function to change a caller's variable or return several results. Large data such as arrays and structures would be expensive to copy, and passing an address is cheap. And hardware registers live at fixed memory addresses, so reading a sensor or driving a pin means reading or writing through a pointer to that address.
In the product: a fitness tracker
Every minute the tracker adds the latest steps to the day's totals. One function updates several of the caller's variables by taking their addresses.
#include <stdio.h>
// Adds a minute of walking to the day's totals.
void add_minute(int steps, int *total_steps, int *total_metres) {
*total_steps += steps;
*total_metres += steps * 75 / 100; // about 75 cm a step
}
int main(void) {
int steps = 0, metres = 0;
add_minute(110, &steps, &metres);
add_minute(95, &steps, &metres);
printf("%d steps, %d m\n", steps, metres);
return 0;
}
Build it: lightest and heaviest in one go
This is step 6 of the module project. A baker portions dough and wants to see, after weighing a batch, the lightest and the heaviest piece. One function can only return one value, so it hands back the other results through pointers.
Lab: Return several results through pointers
min_max(r, n, lo, hi)finds the smallest and the largest of thenweights, and stores them in*loand*hi. It returns 1 on success, or 0 ifnis 0 (and then leaves*loand*hialone).read_weight(out)plays the scale's sensor: it is given a pointer, and when the scale is not overloaded it stores the current weight in*outand returns 1. The current weight is the provided variablecurrent_g; above 5000 g it is overloaded, so return 0 and leave*outalone.
Going deeper
Interview question: A fitness tracker counts steps when the wearer is driving. How would you fix it?
Answer: Steps are counted from accelerometer peaks, and a bumpy road produces peaks too. Require a regular cadence within a human walking range for several seconds before counting, look at the signal's shape on all three axes rather than its size alone, and use other context (a phone reporting driving speed, or a GPS speed above walking pace) to suppress counting. Then test it on recorded data from real drives and walks.
Interview question: What is a null pointer and why initialise pointers to NULL?
Answer: A null pointer is a pointer that deliberately points to nothing, written NULL. Initialising pointers to NULL means an unused pointer has a known value that can be checked (if (p != NULL)) instead of a random address. Dereferencing NULL is still a bug, but on many systems it faults immediately at a recognisable address, which is much easier to find than corruption from a random pointer.
Interview question: Why do functions like scanf take &x rather than x?
Answer: Because C passes arguments by value, scanf receiving x would get a copy of its current value and could not change the caller's variable. Passing &x, the address of x, lets scanf write the converted value into the variable itself. Any function that needs to give back more than one result, or modify the caller's data, uses this pattern.
Explain it: In your own words, explain what *p = 5; does when p holds the address of a variable x.
Answer: p holds the address where x is stored. The * in front of p means follow that address to the object it points to, so *p = 5 stores 5 into the memory at that address, which is x itself. After the statement, x is 5 even though the name x does not appear in it. Reading *p would likewise give the current value of x.