Elektrine lite

← Feed

@brettm@swarm.coiloptic.org

Post #3453968

2026-06-28 19:35 UTC

Questions about pointers: I'm learning a bit of Zig and have never used pointers before (I do understand the general concept of how to use them, that they point to address etc). afaik using pointers is faster than operating on/ looking up the variables themselves? My main confusion (using example below) is, why dereference the pointer with 'ptr.*' in the 3rd line? Why not just plug in the address directly with '&num'? Is that for performance too? Otherwise the code seems convoluted for no reason. var num: u8 = 12; const ptr: *u8 = # std.testing.expectEqual(ptr.*, num); #ziglang

Replies (1)

  • @brettm a variable will often turn into a pointer, depending on where it is stored As for dereferencing, consider the x86 assembly: cmp a, b where a, b are constants compares a and b, whereas cmp [a], b reads a to get an address, then reads memory at the address. The constant is hardcoded in the machine code, except one is a memory address whilst the other is an address. A pointer is an address. ([a] is the Intel assembly syntax for dereferencing a pointer, Zig has a.*, C has *a...)

    Open ##3453967