(cpp) Pointers
21 Apr 2024
What are pointers?
Pointers, point to memory addresses, pretty self explanatory.
- Declare them with
int *ptr;
- Initialise them with
ptr = &x
- Dereference them (get the inner value)
int val = *ptr
- Create and delete memory with
new
anddelete
How do you deal with pointers?
Cpp also provides some nice syntactic sugar to access variables through pointers easier.
Instead of
- dereferencing the pointer and access a variable:
(*ptr).x
- You can just do
ptr->x
Ain’t that nice!