Question regarding address and pointer

I’m curious if you have tried other built-ins, such as @intFromPtr?

The documentation isn’t as clear as I’d like it to be, but it may be worth checking out:

@intFromPtr:

Converts value to a usize which is the address of the pointer. value can be *T or ?*T.

To convert the other way, use @ptrFromInt

I’m running this code here:

var return_value: [2]i32 = [_]i32{0} ** 2;

const print = @import("std").debug.print;

pub fn main() !void 
{
    const p1: *i32 = &return_value[0];

    const p2: *[2]i32 = &return_value;

    print("\n\n{any}\n\n{any}\n\n", .{@intCast(i32, @intFromPtr(p1)), @intCast(i32, @intFromPtr(p2))});
}

And here is what it produces:

2416648

2416648

Just a note - here in, @intCast is changing so that the first argument will not be necessary: Major zig language update just landed: removal of destination type from all cast builtins - #4 by AndrewCodeDev

1 Like