• Home
  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • React
      • Introduction to React
      • React Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
  • Interactive Training
  • Pricing
  • Navigate
    • Home
    • Reading Grounds
    • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds

Courses

  • Python
    • Introduction to Python
    • Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • React
    • Introduction to React
    • React Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Zig Developer - Lesson 3

Lesson 3 of 85

Pointer Dereferencing

Lesson Progress: 0%

Lesson Progress: 0%
Pointer Dereferencing
Lesson Incomplete
← Previous: Address Of Operator
Lesson 3 of 85
Next: Single Item Pointers →

Dereferencing a pointer means accessing the value stored at the memory address the pointer holds. In Zig, you use the .* operator to dereference a pointer. If you have a pointer p of type *T, then p.* gives you the value of type T. Dereferencing a mutable pointer allows you to read or write the underlying value. Zig ensures that dereferencing is safe by never allowing null pointers in safe code.

Code Example
const std = @import("std");

pub fn main() void {
    const x: i32 = 10;
    const p: *const i32 = &x;
    std.debug.print("{d}", .{p.*});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • p.* reads the value that the pointer p points to.
  • The type of p.* is the pointed-to type, i32 in this case.
  • Dereferencing a *const T gives a read-only view of the value.
  • You can use p.* anywhere you would use the value directly.
Code Example
const std = @import("std");

pub fn main() void {
    var y: i32 = 5;
    const p: *i32 = &y;
    p.* = 20;
    std.debug.print("{d}", .{y});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Through a mutable pointer *T, you can assign to p.*.
  • p.* = p.* + 3 reads the current value, adds 3, and writes it back.
  • The change is reflected in the original variable val.
  • Dereferencing for writing requires the pointer itself to be mutable (*T not *const T).
Code Example
const std = @import("std");

const Point = struct {
    x: i32,
    y: i32,
};

pub fn main() void {
    const pt = Point{ .x = 3, .y = 7 };
    const p: *const Point = &pt;
    std.debug.print("{d}", .{p.*.x + p.*.y});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A pointer to a struct uses p.*.field to access fields.
  • p.*.x = 10 modifies the field through the dereferenced pointer.
  • The struct type is part of the pointer type: *Point.
  • You can modify any mutable field through a pointer to the struct.
Code Example
const std = @import("std");

pub fn main() void {
    const val: f64 = 2.5;
    const p: *const f64 = &val;
    std.debug.print("{d}", .{p.*});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Use pa.* == pb.* to compare the values behind two pointers.
  • This compares the pointed-to values, not the pointer addresses themselves.
  • The result is a boolean that can be stored or used in conditions.
  • Printing a boolean uses the {any} format specifier.
Code Example
const std = @import("std");

pub fn main() void {
    var x: u8 = 0;
    const p: *u8 = &x;
    p.* = 255;
    std.debug.print("{d}", .{x});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • With a double pointer *const *const T, dereference twice to reach the value.
  • pp.* gives the inner pointer p.
  • pp.*.* gives the original value x.
  • Each .* removes one level of indirection.
Code Example
const std = @import("std");

pub fn main() void {
    const a: i32 = 4;
    const b: i32 = 3;
    const pa = &a;
    const pb = &b;
    std.debug.print("{d}", .{pa.* * pb.*});
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Zig Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Through a struct pointer, you can modify multiple fields using p.*.a and p.*.b.
  • Each field access through the pointer directly modifies the original struct.
  • The changes are visible when you read the original struct's fields.
  • This approach avoids copying the entire struct when you only need to update a few fields.

Test Incomplete

What operator is used to dereference a pointer in Zig?

Question #

1/15

Score

0/0 - 0.0 %