- Pointer holds memory address of value
*T
is a pointer to aT
value
The *
operator itself denotes the pointer’s underlying value.
Therefore, while p
in the example is the memory address, *p
returns the value “behind” the memory address.
When to use pointers
There are mainly two use-cases for pointers:
- If you need direct access to the value
- If copying a value to a function or method is too expensive, e. g. for huge strings. Then using a pointer for passing the value will make the function much faster
Next chapter: Structs