my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

Correct conventional Zig reference vs value passing re #89

Dave Gauer (Jul 31, 2022, 3:58 PM EDT) dee6a96d 18c761d8

+49 -19
+2 -1
build.zig
··· 240 240 }, 241 241 .{ 242 242 .main_file = "043_pointers5.zig", 243 - .output = "Wizard (G:10 H:100 XP:20)", 243 + .output = "Wizard (G:10 H:100 XP:20)\n Mentor: Wizard (G:10000 H:100 XP:2340)", 244 + 244 245 }, 245 246 .{ 246 247 .main_file = "044_quiz5.zig",
+10 -1
exercises/042_pointers4.zig
··· 1 1 // 2 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! 3 + // able to do before: pass a value by reference to a function. 4 + // 5 + // Why would we wish to pass a pointer to an integer variable 6 + // rather than the integer value itself? Because then we are 7 + // allowed to *change* the value of the variable! 8 + // 9 + // +-----------------------------------------------+ 10 + // | Pass by reference when you want to change the | 11 + // | pointed-to value. Otherwise, pass the value. | 12 + // +-----------------------------------------------+ 4 13 // 5 14 const std = @import("std"); 6 15
+29 -11
exercises/043_pointers5.zig
··· 1 1 // 2 - // Passing integer pointers around is generally not something you're going 3 - // to do. Integers are cheap to copy. 4 - // 5 - // But you know what IS useful? Pointers to structs: 2 + // As with integers, you can pass a pointer to a struct when you 3 + // will wish to modify that struct. Pointers are also useful when 4 + // you need to store a reference to a struct (a "link" to it). 6 5 // 7 6 // const Vertex = struct{ x: u32, y: u32, z: u32 }; 8 7 // ··· 16 15 // YES: pv.x 17 16 // NO: pv.*.x 18 17 // 19 - // We can write functions that take pointer arguments: 18 + // We can write functions that take pointers to structs as 19 + // arguments. This foo() function modifies struct v: 20 20 // 21 21 // fn foo(v: *Vertex) void { 22 22 // v.x += 2; ··· 24 24 // v.z += 7; 25 25 // } 26 26 // 27 - // And pass references to them: 27 + // And call them like so: 28 28 // 29 29 // foo(&v1); 30 30 // 31 - // 32 31 // Let's revisit our RPG example and make a printCharacter() function 33 - // that takes a Character pointer. 32 + // that takes a Character by reference and prints it...*and* 33 + // prints a linked "mentor" Character, if there is one. 34 34 // 35 35 const std = @import("std"); 36 36 ··· 44 44 const Character = struct { 45 45 class: Class, 46 46 gold: u32, 47 - health: u8 = 100, // <--- You can also provide fields a default value! 47 + health: u8 = 100, // You can provide default values 48 48 experience: u32, 49 + 50 + // I need to use the '?' here to allow for a null value. But 51 + // I don't explain it until later. Please don't tell anyone. 52 + mentor: ?*Character = null, 49 53 }; 50 54 51 55 pub fn main() void { 52 - var glorp = Character{ 56 + var mighty_krodor = Character{ 57 + .class = Class.wizard, 58 + .gold = 10000, 59 + .experience = 2340, 60 + }; 61 + 62 + var glorp = Character{ // Glorp! 53 63 .class = Class.wizard, 54 64 .gold = 10, 55 65 .experience = 20, 66 + .mentor = &mighty_krodor, // Glorp's mentor is the Mighty Krodor 56 67 }; 57 68 58 69 // FIX ME! 59 - // Please pass our Character "glorp" to printCharacter(): 70 + // Please pass Glorp to printCharacter(): 60 71 printCharacter(???); 61 72 } 62 73 ··· 78 89 c.health, 79 90 c.experience, 80 91 }); 92 + 93 + // Checking an "optional" value and capturing it will be 94 + // explained later (this pairs with the '?' mentioned above.) 95 + if (c.mentor) |mentor| { 96 + std.debug.print(" Mentor: ", .{}); 97 + printCharacter(mentor); 98 + } 81 99 }
+3 -3
exercises/047_methods.zig
··· 10 10 // pub fn hello() void { 11 11 // std.debug.print("Foo says hello!\n", .{}); 12 12 // } 13 - // } 13 + // }; 14 14 // 15 15 // 2. A function that is a member of a struct is a "method" and is 16 16 // called with the "dot syntax" like so: ··· 23 23 // const Bar = struct{ 24 24 // number: u32, 25 25 // 26 - // pub fn printMe(self: *Bar) void { 26 + // pub fn printMe(self: Bar) void { 27 27 // std.debug.print("{}\n", .{self.number}); 28 28 // } 29 - // } 29 + // }; 30 30 // 31 31 // (Actually, you can name the first parameter anything, but 32 32 // please follow convention and use "self".)
+3 -1
patches/gollum.sh
··· 22 22 if [ ! -f $b ]; then echo "No $f! We hates it!"; exit 1; fi 23 23 if [ ! -f $a ]; then echo "No $a! Where is it? Where is the answer, precious?"; exit; fi 24 24 25 - echo "Hissss!\tbefore: '$b'\n\t after: '$a'\n\t patch: '$p'\n" 25 + echo "Hissss! before: '$b'" 26 + echo " after: '$a'" 27 + echo " patch: '$p'" 26 28 27 29 diff $b $a > $p 28 30
+1 -1
patches/patches/042_pointers4.patch
··· 1 - 31c31 1 + 40c40 2 2 < ??? = 5; // fix me! 3 3 --- 4 4 > x.* = 5; // fix me!
+1 -1
patches/patches/043_pointers5.patch
··· 1 - 60c60 1 + 71c71 2 2 < printCharacter(???); 3 3 --- 4 4 > printCharacter(&glorp);