A pointer is a type that stores the address of an object.
For example, int*
is a pointer to an int, and will store the address of an int
.
- the address-of operator
&
creates a pointer to an object - the indirection operator
*
accesses the pointed-to object
Using these operators is called referencing and dereferencing, respectively.
int x = 50;
int* p = &x; // p == &x, *p == 50, x == 50
*p = 10; // p == &x, *p == 10, x == 10
p = NULL; // p == NULL, *p == ??, x == 10
Note: use nullptr instead of NULL in C++
- learncpp.com: Introduction to pointers
- cppreference.com: Pointer declaration
- stackoverflow.com: What exactly is nullptr?
- cdecl+ to experiment with pointer syntax
howto pointer
?creditFooter 164892896514801664