aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/04_arrays.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-01-06 17:41:53 -0500
committerDave Gauer <dave@ratfactor.com>2021-01-06 17:41:53 -0500
commitb9b89737fca7cc80b7d1b1527445e0ec71b25dda (patch)
treeb922cc620d25392892b91a0b634925e828edfcd7 /04_arrays.zig
parent30ef32e238465e67321d15c562d6313043dd12d7 (diff)
Added first quiz
Diffstat (limited to '04_arrays.zig')
-rw-r--r--04_arrays.zig24
1 files changed, 13 insertions, 11 deletions
diff --git a/04_arrays.zig b/04_arrays.zig
index 2e3c208..a509800 100644
--- a/04_arrays.zig
+++ b/04_arrays.zig
@@ -1,26 +1,28 @@
//
-// Let's learn some array basics. Arrays literals are declared with:
+// Let's learn some array basics. Arrays are declared with:
//
-// [size]<type>{ values };
+// const foo [size]<type> = [size]<type>{ values };
//
// When Zig can infer the size of the array, you can use '_' for the
-// size like so:
+// size. You can also let Zig infer the type of the value so the
+// declaration is much less verbose.
//
-// [_]<type>{ values };
+// const foo = [_]<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 some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
+
+ // Individual values can be set with '[]' notation. Let's fix
+ // the first prime (it should be 2!):
+ some_primes[0] = 2;
+
+ // Individual values can also be accessed with '[]' notation.
const first = some_primes[0];
- // Looks like we need to complete this expression:
+ // Looks like we need to complete this expression (like 'first'):
const fourth = ???;
// Use '.len' to get the length of the array: