liveBook · Manning

Pointer

As explained in Systems Programming with Zig

A pointer type represents values that refer to memory locations or other objects.

A pointer is a memory address: a number identifying a location in a process’s memory. Zig uses raw machine pointers to give programs direct control over data, without hiding memory access behind extra abstractions. section 2.2

That control comes with sharp edges. A pointer can lead to uninitialized memory or data that is no longer valid. Accessing an address outside the memory permitted by the operating system can cause a segmentation fault. Zig’s safety checks and pointer types help make unsafe memory operations harder to write. section 2.2 section 1.2.3

Choose a type that describes what the address refers to. A single-item pointer addresses one element; a many-item pointer addresses an unknown number of elements. A pointer to an array carries a compile-time element count, while a slice carries a runtime count. An array itself is a value, not a pointer. section 2.2.6

Table 2.4 Pointers and related types
Name Spelling Operations Explanation

Single-Item Pointers

*T, *const T

Dereferencing, taking the slice [0..1]

The address of a single element in memory.

Many-Item Pointers

[*]T, [*]const T

Indexing, arbitrary slicing

The address of some number of elements in memory. The number is unknown to the type checker.

Arrays

[N]T

Indexing, arbitrary slicing

A value which holds multiple elements in memory, where the number of elements is known at comptime.

Single-item pointers to arrays

*[N]T, *const [N]T

Indexing, arbitrary slicing

A pointer to multiple elements in memory, where the number of elements is known at comptime.

Slices

[]T, []const T

Indexing, arbitrary slicing

A pointer to multiple elements in memory, where the number of elements is known at runtime.

Compare pointer, array, and slice spellings alongside the operations each supports. section 2.2.6

Pointers support tasks from mutating data and referencing arrays to representing strings, managing memory, and using function pointers for polymorphism. Zig also distinguishes single-item and multiple-item pointers and requires null to be handled through its type system. section 2.2 section 1.2.1

How Systems Programming with Zig covers it

How deep it goes
Defines raw machine pointers, explains memory hazards, and compares pointer-related type spellings and operations.
Examples
Compares single-item pointers, many-item pointers, array pointers, arrays, and slices in a type-and-operation table.
What you can do after
Evaluate pointer-related types by element count and supported operations, and recognize common invalid-memory hazards.
How it connects
Connects pointers to arrays, slices, strings, memory management, null handling, and polymorphism.

Also explained in

Compare how 4 books explain pointer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.