Pointers
Intermediate · Types & Methods
A pointer holds the memory address of a value. &x takes an address; *p
dereferences. Pointers let functions mutate the caller’s data and avoid copying
large structs.
- pointers1 — read/write an int through
*p - pointers2 — mutate a struct via
*Struct - pointers3 — value copy vs pointer: only a pointer reaches the caller
Resources
Section titled “Resources”Exercises
Section titled “Exercises”pointers1 test
Section titled “pointers1 test”A pointer holds the address of a value. &x takes the address; *p reads or
Show hint
Dereference with * to write through the pointer:
*p = *p * 2pointers2 test
Section titled “pointers2 test”Passing a *struct lets a function mutate the caller’s struct. Go
Show hint
Go auto-dereferences a struct pointer for field access:
a.Balance += amountpointers3 test
Section titled “pointers3 test”A value parameter receives a COPY, so changes don’t reach the caller.
Show hint
Increment the field through the pointer receiver:
c.N++