aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-01-03 20:34:26 -0500
committerDave Gauer <dave@ratfactor.com>2021-01-03 20:34:26 -0500
commit87541c0c8bc0969e174c6877a69750445ac1865e (patch)
treeea93c1a1e7d574d15e0fd34652a277572a8bd0e7
parentb3f74d9c30b7adc6d5cc971b50b8b8a512fe3448 (diff)
downloadziglings-87541c0c8bc0969e174c6877a69750445ac1865e.tar.gz
ziglings-87541c0c8bc0969e174c6877a69750445ac1865e.tar.bz2
ziglings-87541c0c8bc0969e174c6877a69750445ac1865e.tar.xz
ziglings-87541c0c8bc0969e174c6877a69750445ac1865e.zip
Added Ex. 4 arrays
-rw-r--r--04_arrays.zig31
-rw-r--r--README.md2
-rwxr-xr-xziglings3
3 files changed, 34 insertions, 2 deletions
diff --git a/04_arrays.zig b/04_arrays.zig
new file mode 100644
index 0000000..2e3c208
--- /dev/null
+++ b/04_arrays.zig
@@ -0,0 +1,31 @@
+//
+// Let's learn some array basics. Arrays literals are declared with:
+//
+// [size]<type>{ values };
+//
+// When Zig can infer the size of the array, you can use '_' for the
+// size like so:
+//
+// [_]<type>{ values };
+//
+const std = @import("std");
+
+pub fn main() void {
+ const some_primes = [_]u8{ 2, 3, 5, 7, 11, 13, 17, 19 };
+
+ // Array values are accessed using square bracket '[]' notation.
+ //
+ // (Note that when Zig can infer the type (u8 in this case) of a
+ // value, we don't have to manually specify it.)
+ //
+ const first = some_primes[0];
+
+ // Looks like we need to complete this expression:
+ const fourth = ???;
+
+ // Use '.len' to get the length of the array:
+ const length = some_primes.???;
+
+ std.debug.print("First: {}, Fourth: {}, Length: {}\n",
+ .{first, fourth, length});
+}
diff --git a/README.md b/README.md
index 570cf85..d83988f 100644
--- a/README.md
+++ b/README.md
@@ -60,7 +60,7 @@ Planned exercises:
* [x] Hello world (main needs to be public)
* [x] Importing standard library
* [x] Assignment
-* [ ] Arrays
+* [x] Arrays
* [ ] If
* [ ] While
* [ ] For
diff --git a/ziglings b/ziglings
index 395d50d..4c6e52c 100755
--- a/ziglings
+++ b/ziglings
@@ -67,7 +67,8 @@ function check_it {
check_it 01_hello.zig "Hello world" "Note the error: the source file has a hint for fixing 'main'."
check_it 02_std.zig "Standard Library"
-check_it 03_assignment.zig "55 314159 -11"
+check_it 03_assignment.zig "55 314159 -11" "There are three mistakes in this one!"
+check_it 04_arrays.zig "Fourth: 7, Length: 8" "There are two things to complete here."
echo
echo " __ __ _ "