• 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 2

Lesson 2 of 85

Address Of Operator

Lesson Progress: 0%

Lesson Progress: 0%
Address Of Operator
Lesson Incomplete
← Previous: Pointer Basics
Lesson 2 of 85
Next: Pointer Dereferencing →

The & (address-of) operator produces a pointer to a variable. In Zig, you use & to create a pointer from any mutable or immutable variable. The resulting pointer has type *T for mutable variables or *const T for immutable ones. Taking the address does not copy the value; it gives you a reference to the original memory location. This operator is the foundation of all pointer work in Zig.

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

pub fn main() void {
    const x: i32 = 5;
    const addr = &x;
    _ = addr;
    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
  • &x computes the memory address of the variable x.
  • The expression &x has type *const i32 because x is immutable.
  • You can store the pointer in a variable and dereference it later.
  • The address-of operator does not allocate new memory; it reuses the existing variable.
Code Example
const std = @import("std");

pub fn main() void {
    const arr = [_]u8{ 1, 2, 3 };
    const p = &arr[1];
    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
  • You can take the address of any named variable regardless of its type.
  • The pointer type matches the type of the variable: *const u8 for a u8.
  • Each pointer is typed and knows the size and alignment of its referent.
  • Zig does not allow taking the address of a temporary or literal value.
Code Example
const std = @import("std");

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

pub fn main() void {
    const pt = Point{ .x = 10, .y = 20 };
    const px = &pt.x;
    std.debug.print("{d}", .{px.*});
}

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
  • You can take the address of an array element with &arr[1].
  • The pointer type is *i32 because the array and its elements are mutable.
  • Modifying through the pointer changes the element in the original array.
  • Indexing with arr[1] accesses the element; &arr[1] gets its address.
Code Example
const std = @import("std");

pub fn main() void {
    const x: i32 = 100;
    const p: *const i32 = &x;
    std.debug.print("{any}", .{p == &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
  • Use &pt.x to get a pointer to a specific struct field.
  • The pointer type matches the field type, not the struct type.
  • Modifying px.* changes pt.x in place.
  • You can take the address of any mutable field inside a struct.
Code Example
const std = @import("std");

pub fn main() void {
    const a: i32 = 1;
    const b: i32 = 2;
    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
  • Taking the address of the same variable twice gives two pointers to the same location.
  • p1 and p2 hold the same address and reference the same memory.
  • Writing through p1 is visible when reading through p2.
  • Multiple pointers to the same value are called aliasing pointers.
Code Example
const std = @import("std");

pub fn main() void {
    const val: bool = true;
    const p = &val;
    std.debug.print("{any}", .{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
  • String literals in Zig have type *const [N]u8.
  • &msg gives a pointer to the whole string array.
  • Dereferencing the pointer with p.* gives the entire array.
  • Use {s} to print the array as a string.

Test Incomplete

What does the & operator produce in Zig?

Question #

1/15

Score

0/0 - 0.0 %