Hey all, hoping someone can help me understand how zig returns values. I wrote up a test case to help me understand and I'm confused by the results:
fn getFromList(list: *ArrayList(u32), ind: u32) u32 {
return list.items[ind];
}
pub fn main() !u8 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var ar = ArrayList(u32).init(allocator);
ar.ensureTotalCapacity(10) catch unreachable;
ar.appendNTimes(0, 10) catch unreachable;
const itemsAddress = &ar.items;
const itemAddress = &ar.items[5];
const throughFunc: *const u32 = &getFromList(&ar, 5);
std.debug.print("{}, {}, {}", .{ itemsAddress, itemAddress, throughFunc });
return 0;
}
I get the following output:
[]u32@16fd9ef98, u32@1001ac02c, u32@16fd9efec
Which I find strange, as it seems like the return through the function returns the actual memory location while taking the address of the items dereference doesn't? Also, why does the type of throughFunc here have to be *const u32 instead of *u32? I'd appreciate any help and pointers to documentation sections, thanks!