summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authorChris Boesch <chrboesch@noreply.codeberg.org>2026-05-22 21:58:19 +0200
committerChris Boesch <chrboesch@noreply.codeberg.org>2026-05-22 21:58:19 +0200
commit1d965491c107489b2fcb1d9e304ad7e71b9d391e (patch)
tree6f32cdd09d64effb2841d84b7c3b039cb5db7451 /exercises
parent926fc04d81c62832604c5ebb6bed1f3b01d6189e (diff)
added expalantion for const pointer
Diffstat (limited to 'exercises')
-rw-r--r--exercises/040_pointers2.zig16
1 files changed, 16 insertions, 0 deletions
diff --git a/exercises/040_pointers2.zig b/exercises/040_pointers2.zig
index a4852f6..e220336 100644
--- a/exercises/040_pointers2.zig
+++ b/exercises/040_pointers2.zig
@@ -27,3 +27,19 @@ pub fn main() void {
std.debug.print("a: {}, b: {}\n", .{ a, b.* });
}
+//
+// A look into the future:
+// When you allocate memory, you store the returned address in
+// a const var. The pointer itself never changes — it always
+// refers to the same allocation — but you can still read and
+// write the data it points to.
+//
+// Example:
+//
+// const buf = try allocator.alloc(u8, 1024);
+// buf[0] = 42; // fine: the *contents* are mutable
+//
+// Note:
+// Passing this pointer to a function is cheap: it's just an address
+// copied on the stack. The caller can work with the data without
+// needing to know where it came from or how it was allocated.