078_sentinels3.zig 851 B

123456789101112131415161718192021222324252627
  1. //
  2. // We were able to get a printable string out of a many-item
  3. // pointer by using a slice to assert a specific length.
  4. //
  5. // But can we ever GO BACK to a sentinel-terminated pointer
  6. // after we've "lost" the sentinel in a coercion?
  7. //
  8. // Yes, we can. Zig's @ptrCast() builtin can do this. Check out
  9. // the signature:
  10. //
  11. // @ptrCast(comptime DestType: type, value: anytype) DestType
  12. //
  13. // See if you can use it to solve the same many-item pointer
  14. // problem, but without needing a length!
  15. //
  16. const print = @import("std").debug.print;
  17. pub fn main() void {
  18. // Again, we've coerced the sentinel-terminated string to a
  19. // many-item pointer, which has no length or sentinel.
  20. const data: [*]const u8 = "Weird Data!";
  21. // Please cast 'data' to 'printable':
  22. const printable: [*:0]const u8 = ???;
  23. print("{s}\n", .{printable});
  24. }