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 |
|
Dereferencing, taking the slice |
The address of a single element in memory. |
Many-Item Pointers |
|
Indexing, arbitrary slicing |
The address of some number of elements in memory. The number is unknown to the type checker. |
Arrays |
|
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 |
|
Indexing, arbitrary slicing |
A pointer to multiple elements in memory, where the number of elements is known at comptime. |
Slices |
|
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



