039_pointers.zig 1021 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // Check this out:
  3. //
  4. // var foo: u8 = 5; // foo is 5
  5. // var bar: *u8 = &foo; // bar is a pointer
  6. //
  7. // What is a pointer? It's a reference to a value. In this example
  8. // bar is a reference to the memory space that currently contains the
  9. // value 5.
  10. //
  11. // A cheatsheet given the above declarations:
  12. //
  13. // u8 the type of a u8 value
  14. // foo the value 5
  15. // *u8 the type of a pointer to a u8 value
  16. // &foo a reference to foo
  17. // bar a pointer to the value at foo
  18. // bar.* the value 5 (the dereferenced value "at" bar)
  19. //
  20. // We'll see why pointers are useful in a moment. For now, see if you
  21. // can make this example work!
  22. //
  23. const std = @import("std");
  24. pub fn main() void {
  25. var num1: u8 = 5;
  26. const num1_pointer: *u8 = &num1;
  27. var num2: u8 = undefined;
  28. // Please make num2 equal 5 using num1_pointer!
  29. // (See the "cheatsheet" above for ideas.)
  30. num2 = ???;
  31. std.debug.print("num1: {}, num2: {}\n", .{ num1, num2 });
  32. }