Post B7p0xAlNPqxsaAb7Sa by brettm@swarm.coiloptic.org
 (DIR) More posts by brettm@swarm.coiloptic.org
 (DIR) Post #B7nlvX7jcFLusnr7iK by brettm@swarm.coiloptic.org
       0 likes, 0 repeats
       
       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
       
 (DIR) Post #B7p0nJlx45NZd1AS1o by sofia@bottom.business
       0 likes, 0 repeats
       
       @brettm@swarm.coiloptic.org operating on pointers isn't really a thing unless you mean pointer arithmetic to get to another value in contiguous memory, operating on anything a pointer references fundamentally requires dereferencing it, even if it may be syntactically ambiguous in some cases due to zig's automatic dereferencing when accessing fieldsPointers are more performant for passing something to a function or similar contexts where assigning a value directly would entail unnecessarily copying it and the value itself is larger than the size of a pointer (namely complex structs)
       
 (DIR) Post #B7p0nK1u6m8iQUdBtQ by brettm@swarm.coiloptic.org
       0 likes, 0 repeats
       
       @sofia@bottom.business yes that makes sense i do recall reading somewhere in the past about moving the value of the pointing being cheaper than moving all the data around all the time
       
 (DIR) Post #B7p0xAQ6gvx1WCe8J6 by tranquillity@mastodon.minionflo.net
       0 likes, 0 repeats
       
       @brettm a variable will often turn into a pointer, depending on where it is storedAs 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 constant 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...)
       
 (DIR) Post #B7p0xAlNPqxsaAb7Sa by brettm@swarm.coiloptic.org
       0 likes, 0 repeats
       
       @tranquillity@mastodon.minionflo.net it does make sense to think about the underlying assembly too thanks!