Skip to content

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

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 * 2

Source

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 += amount

Source

A value parameter receives a COPY, so changes don’t reach the caller.

Show hint
Increment the field through the pointer receiver:
c.N++

Source