Created
July 19, 2020 17:26
-
-
Save manikawnth/e6444f11733f5a334558d36a0adb7206 to your computer and use it in GitHub Desktop.
Zig stack mem returned from func printing, but not mutating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const Point = struct { | |
x: i32, | |
y: i32, | |
fn print(self: Point) void { | |
std.debug.print("x is {}, y is {}\n", .{self.x, self.y}); | |
} | |
}; | |
pub fn main() void { | |
var p = Point {.x = 32, .y = 45 }; | |
var copyValPtr: *Point = reverse_facade(p); | |
copyValPtr.print(); //pointer available here | |
reverse(copyValPtr); // not available here | |
copyValPtr.print(); | |
} | |
fn reverse_facade(p: Point) *Point { | |
var q = Point{.x = p.x, .y =p.y}; //copy | |
var ptr = &q; | |
return ptr; //escapes? | |
} | |
fn reverse(p: *Point) void{ | |
const temp = p.y; | |
p.y = p.x; | |
p.x = temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And here is the output