2021-03-06 21:31:02 -05:00
|
|
|
//
|
|
|
|
// You can also make pointers to multiple items without using a slice.
|
|
|
|
//
|
|
|
|
// var foo: [4]u8 = [4]u8{ 1, 2, 3, 4 };
|
|
|
|
// var foo_slice: []u8 = foo[0..];
|
|
|
|
// var foo_ptr: [*]u8 = &foo;
|
|
|
|
//
|
|
|
|
// The difference between foo_slice and foo_ptr is that the slice has
|
|
|
|
// a known length. The pointer doesn't. It is up to YOU to keep track
|
|
|
|
// of the number of u8s foo_ptr points to!
|
|
|
|
//
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
pub fn main() void {
|
2021-03-10 20:24:23 -05:00
|
|
|
// Take a good look at the array type to which we're coercing
|
|
|
|
// the zen12 string (the REAL nature of strings will be
|
|
|
|
// revealed when we've learned some additional features):
|
2021-03-06 21:31:02 -05:00
|
|
|
const zen12: *const [21]u8 = "Memory is a resource.";
|
|
|
|
//
|
2021-03-10 20:24:23 -05:00
|
|
|
// It would also have been valid to coerce to a slice:
|
|
|
|
// const zen12: []const u8 = "...";
|
2021-03-06 21:31:02 -05:00
|
|
|
//
|
2021-03-13 16:27:14 -05:00
|
|
|
// Now let's turn this into a "many-item pointer":
|
2021-03-07 10:08:07 -05:00
|
|
|
const zen_manyptr: [*]const u8 = zen12;
|
2021-03-06 21:31:02 -05:00
|
|
|
|
2021-03-07 10:08:07 -05:00
|
|
|
// It's okay to access zen_manyptr just like an array or slice as
|
2021-03-06 21:31:02 -05:00
|
|
|
// long as you keep track of the length yourself!
|
|
|
|
//
|
|
|
|
// A "string" in Zig is a pointer to an array of const u8 values
|
2021-03-17 23:22:04 -04:00
|
|
|
// (or a slice of const u8 values, as we saw above). So, we could
|
2021-03-18 10:01:14 -04:00
|
|
|
// treat a "many-item pointer" of const u8 as a string as long as
|
|
|
|
// we can CONVERT IT TO A SLICE. (Hint: we do know the length!)
|
2021-03-06 21:31:02 -05:00
|
|
|
//
|
2021-03-30 21:53:15 -04:00
|
|
|
// Please fix this line so the print statement below can print it:
|
2021-03-07 10:08:07 -05:00
|
|
|
const zen12_string: []const u8 = zen_manyptr;
|
2021-03-06 21:31:02 -05:00
|
|
|
|
|
|
|
// Here's the moment of truth!
|
|
|
|
std.debug.print("{s}\n", .{zen12_string});
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Are all of these pointer types starting to get confusing?
|
|
|
|
//
|
|
|
|
// FREE ZIG POINTER CHEATSHEET! (Using u8 as the example type.)
|
|
|
|
// +---------------+----------------------------------------------+
|
|
|
|
// | u8 | one u8 |
|
|
|
|
// | *u8 | pointer to one u8 |
|
|
|
|
// | [2]u8 | two u8s |
|
|
|
|
// | [*]u8 | pointer to unknown number of u8s |
|
|
|
|
// | [*]const u8 | pointer to unknown number of immutable u8s |
|
|
|
|
// | *[2]u8 | pointer to an array of 2 u8s |
|
|
|
|
// | *const [2]u8 | pointer to an immutable array of 2 u8s |
|
|
|
|
// | []u8 | slice of u8s |
|
|
|
|
// | []const u8 | slice of immutable u8s |
|
|
|
|
// +---------------+----------------------------------------------+
|