I can take the address of an expression without assigning it to a variable first, but apparently it is const:
const std = @import("std");
fn f1() i32 {
return 42;
}
fn f2(p: *const i32) void {
std.debug.print("{}\n", .{p.*});
}
fn f3(p: *i32) void {
p.* += 1;
std.debug.print("{}\n", .{p.*});
}
pub fn main() void {
f2(&f1()); // works
f3(&f1()); // error: expected type '*i32', found '*const i32'
f3(@constCast(&f1())); // this makes it work?
}
Is there a reason why it has to be immutable? As far as I understand it’s just a temporary value stored on the stack.
Is it safe to just constCast it?