040_pointers2.zig 703 B

123456789101112131415161718192021222324252627
  1. //
  2. // It's important to note that variable pointers and constant pointers
  3. // are different types.
  4. //
  5. // Given:
  6. //
  7. // var foo: u8 = 5;
  8. // const bar: u8 = 5;
  9. //
  10. // Then:
  11. //
  12. // &foo is of type "*u8"
  13. // &bar is of type "*const u8"
  14. //
  15. // You can always make a constant pointer to a variable, but you cannot
  16. // make a variable pointer to a constant. This sounds like a logic puzzle,
  17. // but it just means that once data is declared immutable, you can't
  18. // coerce it to a mutable type. It's a safety thing (to prevent mistakes).
  19. //
  20. const std = @import("std");
  21. pub fn main() void {
  22. const a: u8 = 12;
  23. const b: *u8 = &a; // fix this!
  24. std.debug.print("a: {}, b: {}\n", .{ a, b.* });
  25. }