Zig Arrays
An array is a fixed number of values of the same type stored one after another in memory. In Zig the length is part of the type: [5]u8 (five bytes) and [6]u8 (six bytes) are different types, and the compiler always knows how long an array is.
Creating arrays
const std = @import("std");
pub fn main() void {
const primes = [5]u32{ 2, 3, 5, 7, 11 };
const vowels = [_]u8{ 'a', 'e', 'i', 'o', 'u' }; // [_] = count for me
std.debug.print("primes: {any}\n", .{primes});
std.debug.print("{d} vowels, first is {c}\n", .{ vowels.len, vowels[0] });
}primes: { 2, 3, 5, 7, 11 }
5 vowels, first is a
| Syntax | Meaning |
|---|---|
[5]u32{ … } |
array of exactly five u32 |
[_]u32{ … } |
let the compiler count the elements |
arr.len |
number of elements (a usize, known at compile time) |
arr[i] |
element at index i, starting from 0 |
Changing elements
To modify an array, declare it with var:
const std = @import("std");
pub fn main() void {
var scores = [_]i32{ 10, 20, 30 };
scores[1] = 25;
scores[2] += 5;
for (&scores) |*s| {
s.* *= 2; // double every element in place
}
std.debug.print("{any}\n", .{scores});
}{ 20, 50, 70 }
The &scores and |*s| syntax loops over pointers to the elements so they can be changed; you will learn about pointers later. Looping with for (scores) |s| gives you copies, which are read-only.
Bounds checking
Indexing past the end is caught. With a constant index, the compiler refuses outright:
const std = @import("std");
pub fn main() void {
const days = [_][]const u8{ "Mon", "Tue", "Wed" };
std.debug.print("{s}\n", .{days[3]});
}./example.zig:5:37: error: index 3 outside array of length 3
std.debug.print("{s}\n", .{days[3]});
^With an index computed at runtime, a Debug or ReleaseSafe build panics with index out of bounds: index 3, len 3 instead of reading random memory as C would.
Filling and repeating
const std = @import("std");
pub fn main() void {
const zeros = [_]u8{0} ** 6; // repeat: 6 zeros
const pattern = [_]u8{ 1, 2 } ** 3; // 1 2 1 2 1 2
const joined = [_]u8{ 7, 8 } ++ [_]u8{9}; // concatenate
var grid: [4]i16 = @splat(-1); // every element set to -1
grid[0] = 0;
std.debug.print("{any}\n{any}\n{any}\n{any}\n", .{ zeros, pattern, joined, grid });
}{ 0, 0, 0, 0, 0, 0 }
{ 1, 2, 1, 2, 1, 2 }
{ 7, 8, 9 }
{ 0, -1, -1, -1 }
** and ++ work only on values known at compile time, because the result's length must be part of its type.
Arrays are values
Assigning an array copies all of its elements. Changing the copy does not affect the original — arrays behave like numbers, not like references:
const std = @import("std");
pub fn main() void {
const original = [_]u8{ 1, 2, 3 };
var copy = original;
copy[0] = 99;
std.debug.print("original={any} copy={any}\n", .{ original, copy });
std.debug.print("equal: {}\n", .{std.mem.eql(u8, &original, ©)});
}original={ 1, 2, 3 } copy={ 99, 2, 3 }
equal: false
Arrays cannot be compared with == as a whole; use std.mem.eql as shown.
Multidimensional arrays
An array of arrays gives you a grid. Index the row first, then the column:
const board = [3][3]u8{
.{ 1, 2, 3 },
.{ 4, 5, 6 },
.{ 7, 8, 9 },
};
// board[1][2] is 6Tip: Because the length is in the type, a function taking
[3]u8cannot accept a[4]u8. For functions that should work with any length, use a slice — the topic of the next lesson.
Test yourself
What does [_]u8{ 4, 5, 6 } create?
- A slice
- An array whose length (3) is inferred
- A dynamic list
- An empty array
After var b = a; where a is an array, what happens when you change b[0]?
a[0]changes too- Only
bchanges, because arrays are copied by value - Compile error
- Both are freed
What does [_]u8{0} ** 4 produce?
- { 0 }
- { 0, 0, 0, 0 }
- { 4 }
- A compile error