Atlas Robotics
C/C++ ProgrammingIntermediate

What is a pointer? Why embedded programming cannot avoid pointers

Atlasbot · 16/8/2026 · 10 min read

What is a pointer? Why embedded programming cannot avoid pointers
Variables, addresses and pointers

Pointers are often considered the most "scary" concept when learning C — but in embedded programming, they are almost impossible to avoid: every hardware register, every externally mapped memory area, is accessed through address, and the address is what a pointer holds.

Key concepts

Each variable in the program is stored at a specific address in RAM. A pointer is a special variable — instead of storing the value directly, it stores the address of another variable.

uint8_t sensor_value = 42;
uint8_t *p = &sensor_value;  // p stores the ADDRESS of sensor_value, not 42

printf("%d\n", sensor_value); // 42
printf("%p\n", p);            // e.g. 0x2000001c (a RAM address)
printf("%d\n", *p);           // 42 — "go to" the address p is pointing at to get the value

Two operators to remember: & (address-of) takes the address of a variable; * (dereference) goes to that address to read/write the value actually located there.

Why does embedded programming use pointers more?

Three reasons why pointers appear densely in embedded code:

  1. Hardware register access — a GPIO register is just a fixed memory address; To write to it, you must "force" a pointer to point to that address and then write the value through it.
  2. Passing large data without copying — with limited RAM, passing a whole struct 50-byte sensor to the function by value means copying 50 bytes each call; Passing a pointer only takes 4 bytes (address) no matter how large the data is.
  3. Function pointer for callback/interrupt — registers a function for the chip to automatically call when there is an event (interrupt, timer) needs to save the address of that function, which is a pointer.

In short: Pointers aren't "advanced tricks" — they're C's way of representing memory addresses, and in embedded, memory addresses are the only way to touch the actual hardware.

How it works

Classic example: writing directly to a GPIO register by "forcing" a pointer to point to its correct physical address (simplified from what the STM32 HAL does below):

#define GPIOA_ODR  (*(volatile uint32_t *)0x40020014)

GPIOA_ODR |= (1 << 5);   // Turn on the 5th bit → PA5 pin to high level

0x40020014 is not a regular RAM memory location — it is the physical address of the output control register on the chip. The keyword volatile tells the compiler: the value at this address can be changed by hardware at any time, cannot be optimized to skip read/write like with regular RAM variables.

Variable sensor_value (RAM, address 0x2000001c)
         ▲
         │ pointer p stores this address
         │
    uint8_t *p = &sensor_value;

The most common error with pointers is dereference a pointer that has not been assigned a valid address (NULL or "wild" pointer — pointing to random memory) — on a computer that usually causes an immediate crash and is easy to detect; On microcontrollers that do not have a memory protection operating system, this error can cause the chip to "hang" silently or overwrite data in completely unrelated memory areas.

Technical details

Formulas

When adding/subtracting a pointer from an integer (pointer arithmetic), the compiler does not add the number directly to the address — it multiplies the data type size first:

Ith element address = Original address + (i × sizeof(data type))

For example: the uint16_t array starts at address 0x20000010
&arr[3] = 0x20000010 + (3 × 2) = 0x20000016   (sizeof(uint16_t) = 2)

This is why p + 1 on pointer uint16_t* jumps 2 bytes, and on pointer uint32_t* jumps 4 bytes — same expression p + 1, resulting address is different depending on the pointer type.

Parameters

The size of a pointer (not the size of the data it points to) depends on the address bus width of the chip architecture:

ArchitecturePointer size
AVR 8-bit (Arduino Uno)2 byte
ARM Cortex-M 32-bit (STM32, RP2040)4 byte
Xtensa 32-bit (ESP32)4 byte
Typical 64-bit computer8 byte

Register / Bit / Byte

Function pointers — which point not just to data but to the starting address of a function — are the mechanism behind interrupt vector table: an array of function pointers, each element corresponding to a specific type of interrupt (Timer, UART, external GPIO interrupt...). When an interrupt occurs, the hardware automatically looks up the correct location in this table and jumps to the corresponding ISR function address — essentially "calling a function via pointer" but done by the hardware itself, not the calling code directly.

Practical applications

In sensor drivers/modules that are common to many types of hardware (e.g. supporting both I2C and SPI sensors with the same upper code layer), a common design pattern is the struct containing function pointers — each specific type of hardware "plugs" its own read/write function into that struct, the upper code layer just calls through the pointer without knowing whether it is talking to I2C or SPI. This is how portable HAL libraries (can be used on many different chip lines) separate the general logic from the specific hardware operations, avoiding having to rewrite the entire driver every time you change the type of sensor or board.

Practical example

#include <stdio.h>
#include <stdint.h>

int main() {
    uint16_t arr[4] = {10, 20, 30, 40};
    uint16_t *p = arr;

    for (int i = 0; i < 4; i++) {
        printf("arr[%d] = %d, dia chi = %p\n", i, *(p + i), (void*)(p + i));
    }
    return 0;
}
Output (illustrative address, actual address depends on the system):
arr[0] = 10, dia chi = 0x7ffee2a1b800
arr[1] = 20, dia chi = 0x7ffee2a1b802
arr[2] = 30, dia chi = 0x7ffee2a1b804
arr[3] = 40, dia chi = 0x7ffee2a1b806

The address difference between consecutive elements is exactly 2 (bytes) — matches the formula in the Technical Knowledge section because sizeof(uint16_t) = 2. This is visual proof that p + i does not "jump i address" but "jump i element".

Common mistakes

  • Dereference uninitialized pointer ("wild" pointer) — declare uint8_t *p; then use *p immediately without assigning a valid read/write address to a random memory area, undefined behavior (undefined behavior) — always initialize the pointer to NULL or assign a valid address before using.
  • Forgot volatile when pointing to hardware register — as mentioned in the section Principle of Operation, missing volatile causes the compiler to skip reading/writing the actual value of the register.
  • Confusing pointer addition with normal arithmetic addition — forgetting that p + 1 jumps according to sizeof data type, not exactly 1 byte, leading to wrong address calculation when operating on data types other than 1 byte.
  • Returns a pointer to a local variable of the function — the local variable is released as soon as the function ends, the returned pointer points to a memory area that is no longer valid (dangling pointer) — reusing that pointer after the function has returned causes an error that is difficult to reproduce.

Conclusion

Pointers in embedded programming are not a language feature that can be avoided — they are a fundamental mechanism for accessing registers, transferring data efficiently, and handling interrupts. Understanding how addresses are calculated properly (especially with pointer arithmetic) and always being careful with uninitialized/expired pointers are required skills. The next article in this section is about Struct — a way to group many related variables together, often accompanied by pointers when passing between functions.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

What is a pointer? Why embedded programming cannot avoid pointers — Atlas Robotics