0Pricing
Zig Academy · Lesson

Slices: A Pointer Plus a Length

Reference a window into an array.

Slices: A Pointer Plus a Length is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Slices Exist

Arrays carry a fixed length, but functions often need to work with any length. A slice solves this by describing a flexible run of elements. 🔪

Two Pieces of Data

A slice is really just two things bundled together: a pointer to the first element and a length counting how many follow.

The Slice Type

You write a slice type as []T, with empty brackets and an element type. This []i32 means a slice of signed integers.

var view: []i32 = undefined;

Slices Borrow, Not Own

A slice does not own its memory; it borrows a view into something else, usually an array. The backing data must outlive the slice.

Length Lives at Runtime

Unlike an array, a slice's len is a runtime value. That is exactly what lets one slice type cover many different sizes.

const n = view.len;

From Array to Slice

Take a whole array as a slice with [0..], meaning from index zero to the end. The slice now points into that array.

var data = [_]i32{ 5, 6, 7 };
const s = data[0..];

Indexing a Slice

Read elements through a slice exactly like an array, using square brackets. The index is added to the stored pointer to find the value.

const middle = s[1];

Read-Only Slices

A []const u8 is a slice you may read but not write through. The const protects the borrowed data from accidental changes.

const msg: []const u8 = "hi";

Passing Slices Is Cheap

Handing a slice to a function copies only the pointer and length, never the elements. Big buffers pass with no extra cost.

fn sum(items: []const i32) i32 {
    // walk items here
}

Slices Stay Bounds-Checked

Because a slice knows its length, indexing it is still checked. Going past len triggers a safety panic in safe builds.

Empty Slices Are Valid

A slice can hold zero elements. An empty slice simply has len equal to 0, which is perfectly normal to pass around.

const nothing: []const i32 = &[_]i32{};

Quick Check

Think about what a slice actually stores compared to a fixed array. Which pair describes it?

Recap

A slice is a pointer plus a length that borrows a view into existing data, stays bounds-checked, and passes cheaply by reference. 🎯

Frequently asked questions

Is the “Slices: A Pointer Plus a Length” lesson free?

Yes — the full text of “Slices: A Pointer Plus a Length” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Slices: A Pointer Plus a Length”?

Reference a window into an array. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Slices: A Pointer Plus a Length” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Fixed-Size Arrays
  2. Slices: A Pointer Plus a Length
  3. Slicing with array[start..end]
  4. Iterating and Mutating Slices
← Back to Zig Academy