aboutsummaryrefslogtreecommitdiff
path: root/39_pointers.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-02-08 20:35:28 -0500
committerDave Gauer <dave@ratfactor.com>2021-02-08 20:35:28 -0500
commitcf0920de31e9b5f3c5ba6de19a1b4c8d0c58b907 (patch)
treed0c1af19e12e503a6d720b8e8ef487966e4c7d75 /39_pointers.zig
parentadf5ddb27df7f5a22b0b7d3321dfc8bca1e7937a (diff)
downloadziglings-cf0920de31e9b5f3c5ba6de19a1b4c8d0c58b907.tar.gz
ziglings-cf0920de31e9b5f3c5ba6de19a1b4c8d0c58b907.tar.bz2
ziglings-cf0920de31e9b5f3c5ba6de19a1b4c8d0c58b907.tar.xz
ziglings-cf0920de31e9b5f3c5ba6de19a1b4c8d0c58b907.zip
Added Ex. 38-43 for pointers, updated README
Added topics beyond the language basics from ziglearn.org to the README. That's a lot of exercises. I'd like to keep it under 100, though!
Diffstat (limited to '39_pointers.zig')
-rw-r--r--39_pointers.zig36
1 files changed, 36 insertions, 0 deletions
diff --git a/39_pointers.zig b/39_pointers.zig
new file mode 100644
index 0000000..25b56c6
--- /dev/null
+++ b/39_pointers.zig
@@ -0,0 +1,36 @@
+//
+// Check this out:
+//
+// var foo: u8 = 5; // foo is 5
+// var bar: *u8 = &foo; // bar is a pointer
+//
+// What is a pointer? It's a reference to a value. In this example
+// bar is a reference to the memory space that current contains the
+// value 5.
+//
+// A cheatsheet given the above declarations:
+//
+// u8 the type of a u8 value
+// foo the value 5
+// *u8 the type of a pointer to a u8 value
+// &foo a reference to foo
+// bar a pointer to the value at foo
+// bar.* the value 5 (the dereferenced value "at" bar)
+//
+// We'll see why pointers are useful in a moment. For now, see if you
+// can make this example work!
+//
+const std = @import("std");
+
+pub fn main() void {
+ var num1: u8 = 5;
+ var num1_pointer: *u8 = &num1;
+
+ var num2: u8 = undefined;
+
+ // Please make num2 equal 5 using num1_pointer!
+ // (See the "cheatsheet" above for ideas.)
+ num2 = ???;
+
+ std.debug.print("num1: {}, num2: {}\n", .{num1, num2});
+}