referring to this post, here’s a simple program with both sorts of pointer to a mutable struct instance:
const std = @import("std");
const print = std.log.debug;
const S = struct {
x: u32 = 10,
};
var vp: *S = @constCast(&std.mem.zeroInit(S, .{}));
const cp: *S = @constCast(&std.mem.zeroInit(S, .{}));
pub fn main() void {
// no errors
vp.x = 20;
print("vp.x = {d}", .{vp.x});
// no compile-time error
cp.x = 20;
// runtime crash
print("cp.x = {d}", .{cp.x});
}
i would have thought (hoped??) that i could modify cp.x
– but apparently this crashes at runtime…
FWIW, i also tried using cp.*.x
but with the same results…
this is failing in 0.13.0… in 0.12.0, there’s no failure but the print
output shows 10…
this problem is somewhat related to an earlier post of mine… i was hoping to solve this problem by having const-pointers-mutable-data within my outer comptime struct…
but then i realized some more fundamental isn’t working as i thought…