Lesson 1 of 85
Lesson Progress: 0%
A pointer holds the memory address of a value. In Zig, pointers are explicit and always refer to a valid value at runtime. Every pointer has a type written as *T for a pointer to a single item of type T. Zig does not allow null pointers by default, which eliminates a whole class of bugs. Pointers can point to mutable or immutable data depending on whether you use *const T or *T. Understanding pointers is essential for working with memory, passing data efficiently, and interfacing with C code.
const std = @import("std");
pub fn main() void {
const x: i32 = 42;
const ptr: *const i32 = &x;
std.debug.print("{d}", .{ptr.*});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
& operator takes the address of a variable and produces a pointer.*const i32 is the type of a pointer to a read-only i32.p.* (dot-star) to access the value that the pointer points to.const std = @import("std");
pub fn main() void {
const x: i32 = 7;
const p: *const i32 = &x;
std.debug.print("{any}", .{p == &x});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*const u8 and *const i16 are different types.const std = @import("std");
pub fn main() void {
const val: f64 = 3.14;
const p: *const f64 = &val;
std.debug.print("{d}", .{p.*});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*const [N]T.&arr produces a pointer to the entire array, not just the first element.p.*[index] to access an element through the pointer.const std = @import("std");
pub fn main() void {
const a: u8 = 10;
const b: u8 = 20;
const pa: *const u8 = &a;
const pb: *const u8 = &b;
std.debug.print("{d}", .{pa.* + pb.*});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
&x and &y are distinct pointers.const std = @import("std");
pub fn main() void {
const msg: []const u8 = "hello";
const p: *const []const u8 = &msg;
std.debug.print("{s}", .{p.*});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*T lets you modify the value it points to.p.* changes the underlying variable.val reflects the change because the pointer shares the same memory.const std = @import("std");
pub fn main() void {
const x: bool = true;
const p: *const bool = &x;
std.debug.print("{any}", .{p.*});
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
*const *const T.pp.*.*, to reach the original value..* dereference.Test Incomplete