Pointers
Addresses, arithmetic, const, void pointers and object lifetime.
A pointer is an address with a type
A pointer holds the address of an object. Its type says what kind of object lives there, which decides how many bytes a dereference reads and how far + 1 moves.
| Syntax | Meaning |
|---|---|
int *p; | p can hold the address of an int |
p = &x; | & takes the address of x |
*p | The object p points to (dereference) |
p->member | Shorthand for (*p).member |
NULL | A pointer that points at nothing |
#include <stdio.h>
#include <stdint.h>
static void increment(int32_t *value) {
(*value)++; // changes the caller's variable
}
int main(void) {
int32_t count = 41;
int32_t *p = &count;
increment(p);
printf("count = %d, *p = %d\n", count, *p);
printf("the pointer itself is %u bytes and holds %p\n", (unsigned)sizeof p, (void *)p);
return 0;
}
C passes arguments by value, so a function that must modify its caller's variable, or return more than one result, takes a pointer to it. The address printed is in RAM, which starts at 0x20000000 on this target, just as on a Cortex-M.
Pointer arithmetic
Adding 1 to a pointer advances it by one element, not one byte. p + n is n * sizeof(*p) bytes further on. Subtracting two pointers into the same array gives the number of elements between them.
#include <stdio.h>
#include <stdint.h>
int main(void) {
uint32_t data[4] = { 10, 20, 30, 40 };
uint32_t *p = data; // an array name decays to a pointer to its first element
uint8_t *bytes = (uint8_t *)data;
printf("p + 1 is %u bytes on, bytes + 1 is %u byte on\n",
(unsigned)((uint8_t *)(p + 1) - (uint8_t *)p), (unsigned)((bytes + 1) - bytes));
printf("*(p + 2) = %u, p[2] = %u\n", *(p + 2), p[2]);
uint32_t *end = data + 4; // one past the end: valid to form, not to dereference
uint32_t sum = 0;
for (uint32_t *q = data; q != end; q++) sum += *q;
printf("sum = %u, elements = %d\n", sum, (int)(end - data));
return 0;
}
a[i] is defined as *(a + i). Indexing and pointer arithmetic are the same operation.
Pitfall:
*p++is*(p++): it reads the object, then advances the pointer.(*p)++increments the object. Postfix operators bind tighter than*.
const and pointers
There are two things that can be const: the data and the pointer. Read the declaration from right to left.
| Declaration | Reads as | You may not |
|---|---|---|
const uint8_t *p | pointer to const data | write *p |
uint8_t const *p | the same: const applies to what is on its left, or to its right if nothing is on the left | write *p |
uint8_t *const p | const pointer to data | change p |
const uint8_t *const p | const pointer to const data | do either |
uint8_t *const is the shape of a fixed hardware buffer: the address never changes, the contents do. And const is about the name, not the memory: a const uint8_t * can point at a perfectly writable array; it only stops writes through that pointer.
Use const on every pointer parameter the function only reads. It documents the interface, lets callers pass constant data, and lets the linker put tables in flash instead of RAM (on Arm; on the AVR, const data stays in RAM unless it is marked PROGMEM).
size_t count_zeros(const uint8_t *data, size_t n); // promises not to modify the buffer
void pointers
void * is a pointer with no element type. It converts to and from any object pointer without a cast, which makes it the type for generic interfaces such as memcpy or a callback's context argument. Because it has no size, it cannot be dereferenced or used in arithmetic until it is converted to a real type.
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
// Works for any type: it only ever looks at bytes.
static void fill(void *dst, uint8_t value, size_t n) {
uint8_t *p = dst;
while (n--) *p++ = value;
}
int main(void) {
uint32_t word;
uint16_t pair[2];
fill(&word, 0xAB, sizeof word);
fill(pair, 0x11, sizeof pair);
printf("0x%08X 0x%04X 0x%04X\n", word, pair[0], pair[1]);
return 0;
}
unsigned char (or uint8_t) is the type to use for looking at the bytes of any object.
Pointers to pointers
A function that must change a pointer belonging to its caller takes a pointer to that pointer. Parsers use this to advance a cursor, and linked lists use it to update a link.
#include <stdio.h>
static void skip_spaces(const char **cursor) {
while (**cursor == ' ') (*cursor)++;
}
int main(void) {
const char *line = " SET 42";
const char *p = line;
skip_spaces(&p);
printf("skipped %d characters, now at \"%s\"\n", (int)(p - line), p);
return 0;
}
Lifetime: where most crashes come from
A pointer is only as good as the object it points to. C has three storage durations, and mixing them up is the source of most hard faults.
| Storage | Declared as | Lives until | Typical bug |
|---|---|---|---|
| Automatic (stack) | local variable | the function returns | returning its address |
| Static | global, or static local | the program ends | none, but it is shared state |
| Allocated (heap) | malloc | free is called | use after free, leaks, double free |
Run this. On real hardware it prints garbage or appears to work, depending on what reuses the stack. The simulator checks every access and tells you what went wrong.
#include <stdio.h>
#include <stdint.h>
static const uint8_t *get_config(void) {
uint8_t config[4] = { 1, 2, 3, 4 }; // lives in this function's stack frame
return config; // the frame is gone when the function returns
}
int main(void) {
const uint8_t *cfg = get_config();
printf("%u\n", cfg[0]);
return 0;
}
Now make the array static const and run it again. The data then has a permanent address, and can live in flash.
Other faults the simulator reports, each with the line that caused it:
- Dereferencing
NULLor an uninitialised pointer. - Reading or writing past the end of an array, on the stack, in a global or on the heap.
- Using heap memory after
free, or freeing it twice. - Writing to a string literal, which lives in read-only flash on Arm (on the AVR it is in RAM, but writing it is undefined behaviour all the same).
Alignment
A 32-bit value is normally stored at an address that is a multiple of 4, so casting a byte pointer to uint32_t * at an arbitrary offset and dereferencing it can fault. To read a multi-byte value from anywhere in a buffer, use memcpy or assemble it from bytes. The structs, unions and memory-layout lesson explains alignment, what each core does with an unaligned access, and byte order.
Common mistakes
- Using a pointer before it points at anything. Initialise pointers, to
NULLif nothing better. - Returning the address of a local variable.
- Off-by-one: an array of
nelements has valid indices 0 ton - 1. sizeof(pointer)when you meant the size of the array it points into.- Casting away
constto write to data that lives in flash.
Interview question: What is the difference between a NULL pointer, a dangling pointer and a wild pointer?
Answer: A NULL pointer deliberately points at nothing and can be tested for. A dangling pointer once pointed at a valid object whose lifetime has ended: a returned local, or freed heap memory. A wild pointer was never initialised and holds whatever was in memory. Only the first can be detected in code, which is why pointers should be initialised and set to NULL after their object is freed.
Interview question: Why is casting a uint8_t * buffer to uint32_t * risky?
Answer: Three reasons. Alignment: the buffer may start at an odd address, and an unaligned 32-bit load faults on some cores. Byte order: the result depends on the CPU's endianness, not on the protocol's. Strict aliasing: the compiler may assume a uint32_t * and a uint8_t array do not overlap in the other direction and optimise accordingly. memcpy or explicit shifts avoid all three.
In the product: a USB audio interface
Audio arrives in blocks. A routine walks a block with pointers and applies a gain in place, so no sample is copied.
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
// Halves every sample of a block, in place.
void attenuate(int16_t *samples, size_t count) {
for (int16_t *p = samples; p < samples + count; p++) *p /= 2;
}
int main(void) {
int16_t block[6] = { 1000, -2000, 3000, -4000, 5000, -6000 };
attenuate(block, 6);
for (size_t i = 0; i < 6; i++) printf("%d ", block[i]);
printf("\n");
return 0;
}
Build it: walk through the sensor packets
This is step 4 of the module project. The heart-rate strap and the cadence sensor send their data in one stream of records, each [length][type][payload...], packed one after another in a buffer. The bike computer walks through the buffer with a pointer, record by record, without ever reading past its end.
Lab: Parse packed records with pointer arithmetic
Each record is a length byte (the number of bytes that follow it, at least 1), then a type byte, then length - 1 payload bytes. The types: 0x01 heart rate (payload: 1 byte, bpm), 0x02 cadence (payload: 2 bytes, rpm, low byte first).
Write parse(buf, n, out). Walk a const uint8_t * pointer from buf to buf + n, one record at a time:
- For a heart-rate or cadence record whose payload has the right size, store the value in
out(hrorcadence; a later record replaces an earlier one). - Skip records of any other type, or with the wrong payload size, using their length.
- Stop with -1 if a record's length is 0 or the record would run past the end of the buffer; such a buffer is corrupt. Otherwise return the number of records walked (skipped ones included).
Going deeper
Interview question: How would you move audio between USB and the codec without glitches?
Answer: Use DMA on both sides with double buffering: while one half of a buffer is being sent to the codec, the firmware fills the other half from USB, and pointers swap at each half-transfer interrupt. The USB side and the codec clock drift apart, so the device either tells the host how fast to send (asynchronous feedback) or adjusts the codec's clock slightly. Processing works in place on the buffer through pointers, never copying samples.
Interview question: What does container_of do and why is it useful?
Answer: Given a pointer to a member of a struct, it computes a pointer to the enclosing struct by subtracting the member's offset (from offsetof). It lets generic code, such as a linked list or callback mechanism, work with an embedded member while the user gets back their full object. The Linux kernel uses it everywhere for intrusive lists and driver data.
Interview question: What is the difference between char *p = "abc"; and char a[] = "abc";?
Answer: The first makes p point to a string literal, which may be stored in read-only memory (flash on a microcontroller); modifying it is undefined behaviour, so it should be declared const char *. The second creates a writable array of four bytes initialised with a copy of the characters. sizeof p is the pointer size; sizeof a is 4.
Explain it: In your own words, explain why returning a pointer to a local variable is a bug, even if it seems to work.
Answer: A local variable lives on the stack only while its function runs. When the function returns, that stack space is released and will be reused by the next function call or interrupt, so the returned pointer refers to memory that no longer belongs to the variable. Reading it may give the old value for a while by luck, which is why it seems to work, but the value changes unpredictably as soon as anything else uses the stack. Return the value itself, or use static, caller-provided or allocated storage.