···11//
22// Now let's use pointers to do something we haven't been
33-// able to do before: pass a value by reference to a function!
33+// able to do before: pass a value by reference to a function.
44+//
55+// Why would we wish to pass a pointer to an integer variable
66+// rather than the integer value itself? Because then we are
77+// allowed to *change* the value of the variable!
88+//
99+// +-----------------------------------------------+
1010+// | Pass by reference when you want to change the |
1111+// | pointed-to value. Otherwise, pass the value. |
1212+// +-----------------------------------------------+
413//
514const std = @import("std");
615
+29-11
exercises/043_pointers5.zig
···11//
22-// Passing integer pointers around is generally not something you're going
33-// to do. Integers are cheap to copy.
44-//
55-// But you know what IS useful? Pointers to structs:
22+// As with integers, you can pass a pointer to a struct when you
33+// will wish to modify that struct. Pointers are also useful when
44+// you need to store a reference to a struct (a "link" to it).
65//
76// const Vertex = struct{ x: u32, y: u32, z: u32 };
87//
···1615// YES: pv.x
1716// NO: pv.*.x
1817//
1919-// We can write functions that take pointer arguments:
1818+// We can write functions that take pointers to structs as
1919+// arguments. This foo() function modifies struct v:
2020//
2121// fn foo(v: *Vertex) void {
2222// v.x += 2;
···2424// v.z += 7;
2525// }
2626//
2727-// And pass references to them:
2727+// And call them like so:
2828//
2929// foo(&v1);
3030//
3131-//
3231// Let's revisit our RPG example and make a printCharacter() function
3333-// that takes a Character pointer.
3232+// that takes a Character by reference and prints it...*and*
3333+// prints a linked "mentor" Character, if there is one.
3434//
3535const std = @import("std");
3636···4444const Character = struct {
4545 class: Class,
4646 gold: u32,
4747- health: u8 = 100, // <--- You can also provide fields a default value!
4747+ health: u8 = 100, // You can provide default values
4848 experience: u32,
4949+5050+ // I need to use the '?' here to allow for a null value. But
5151+ // I don't explain it until later. Please don't tell anyone.
5252+ mentor: ?*Character = null,
4953};
50545155pub fn main() void {
5252- var glorp = Character{
5656+ var mighty_krodor = Character{
5757+ .class = Class.wizard,
5858+ .gold = 10000,
5959+ .experience = 2340,
6060+ };
6161+6262+ var glorp = Character{ // Glorp!
5363 .class = Class.wizard,
5464 .gold = 10,
5565 .experience = 20,
6666+ .mentor = &mighty_krodor, // Glorp's mentor is the Mighty Krodor
5667 };
57685869 // FIX ME!
5959- // Please pass our Character "glorp" to printCharacter():
7070+ // Please pass Glorp to printCharacter():
6071 printCharacter(???);
6172}
6273···7889 c.health,
7990 c.experience,
8091 });
9292+9393+ // Checking an "optional" value and capturing it will be
9494+ // explained later (this pairs with the '?' mentioned above.)
9595+ if (c.mentor) |mentor| {
9696+ std.debug.print(" Mentor: ", .{});
9797+ printCharacter(mentor);
9898+ }
8199}
+3-3
exercises/047_methods.zig
···1010// pub fn hello() void {
1111// std.debug.print("Foo says hello!\n", .{});
1212// }
1313-// }
1313+// };
1414//
1515// 2. A function that is a member of a struct is a "method" and is
1616// called with the "dot syntax" like so:
···2323// const Bar = struct{
2424// number: u32,
2525//
2626-// pub fn printMe(self: *Bar) void {
2626+// pub fn printMe(self: Bar) void {
2727// std.debug.print("{}\n", .{self.number});
2828// }
2929-// }
2929+// };
3030//
3131// (Actually, you can name the first parameter anything, but
3232// please follow convention and use "self".)
+3-1
patches/gollum.sh
···2222if [ ! -f $b ]; then echo "No $f! We hates it!"; exit 1; fi
2323if [ ! -f $a ]; then echo "No $a! Where is it? Where is the answer, precious?"; exit; fi
24242525-echo "Hissss!\tbefore: '$b'\n\t after: '$a'\n\t patch: '$p'\n"
2525+echo "Hissss! before: '$b'"
2626+echo " after: '$a'"
2727+echo " patch: '$p'"
26282729diff $b $a > $p
2830