From a5a545259ac471152124da21d7362c45363fe1dc Mon Sep 17 00:00:00 2001 From: jacwil Date: Sat, 13 Jan 2024 20:36:35 -0800 Subject: [PATCH] Make the example more realistic --- build.zig | 2 +- exercises/076_sentinels.zig | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/build.zig b/build.zig index 058adae..c4d274c 100644 --- a/build.zig +++ b/build.zig @@ -913,7 +913,7 @@ const exercises = [_]Exercise{ }, .{ .main_file = "076_sentinels.zig", - .output = "Array:[ 1 2 3 0 5 6 ]. Many-item pointer:[ 1 2 3 ]. Array:[ 30 31 32 20 34 35 ]. Many-item pointer:[ 30 31 32 ].", + .output = "Array:[ 1 2 3 0 5 6 ]. Many-item pointer:[ 1 2 3 ]. Array:[ 5 4 3 ffff 1 0 ]. Many-item pointer:[ 5 4 3 ].", }, .{ .main_file = "077_sentinels2.zig", diff --git a/exercises/076_sentinels.zig b/exercises/076_sentinels.zig index c95b5da..5c1d3c9 100644 --- a/exercises/076_sentinels.zig +++ b/exercises/076_sentinels.zig @@ -49,21 +49,22 @@ pub fn main() void { const ptr: [*:0]u32 = &nums; // Just to show not everything must be zero-terminated, here - // is a space-terminated array of u8 values 👾: - var space = [_:' ']u8{ '0', '1', '2', '3', '4', '5' }; + // is max-value termination (bits are all ones, which can be + // interpereted as a -1 for a signed number type): + var morenums = [_:0xFFFF]u16{ 5, 4, 3, 2, 1, 0 }; - // And here's a space-terminated many-item pointer: - const spaceptr: [*:' ']u8 = &space; + // And here's a max-value terminated many-item pointer: + const morenumsptr: [*:0xFFFF]u16 = &morenums; // For fun, let's replace the value at position 3 with the // sentinel value. This seems kind of naughty. nums[3] = 0; - space[3] = ' '; + morenums[3] = 0xFFFF; - // Now we have a zero-terminated array, a space-terminated - // array, and a couple of many-item pointers that reference - // the same data: sequences of numbers that both end in and - // CONTAIN the sentinel value. + // Now we have a zero-terminated array, a max-value + // terminated array, and a couple of many-item pointers + // that reference the same data: sequences of numbers that + // both end in and CONTAIN the sentinel value. // // Attempting to loop through and print both of these should // demonstrate how they are similar and different. @@ -73,8 +74,8 @@ pub fn main() void { // at the first sentinel value.) printSequence(nums); printSequence(ptr); - printSequence(space); - printSequence(spaceptr); + printSequence(morenums); + printSequence(morenumsptr); print("\n", .{}); }