042_pointers4.zig 794 B

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // Now let's use pointers to do something we haven't been
  3. // able to do before: pass a value by reference to a function!
  4. //
  5. const std = @import("std");
  6. pub fn main() void {
  7. var num: u8 = 1;
  8. var more_nums = [_]u8{ 1, 1, 1, 1 };
  9. // Let's pass a reference to num to our function and print it:
  10. makeFive(&num);
  11. std.debug.print("num: {}, ", .{num});
  12. // Now something interesting. Let's pass a reference to a
  13. // specific array value:
  14. makeFive(&more_nums[2]);
  15. // And print the array:
  16. std.debug.print("more_nums: ", .{});
  17. for (more_nums) |n| {
  18. std.debug.print("{} ", .{n});
  19. }
  20. std.debug.print("\n", .{});
  21. }
  22. // This function should take a reference to a u8 value and set it
  23. // to 5.
  24. fn makeFive(x: *u8) void {
  25. ??? = 5; // fix me!
  26. }