Skip to main content
Object reference vs pointer
Pointer
- You think of pointer as just another type like int, char, float, it takes up a constant amount of spaces in memory, and you can assign values to them.
- The value you assign to them is a memory address. You can interpret the memory address pointed by the pointer using the dereferencing operator (*).
- A pointer can be assigned to point a
NULL
value
- A pointer can be changed to point to any variable of the same type.
- You can do pointer arithmetic, but you cannot with references.
References
- A reference must be initialized when it is declared
- It cannot be NULL
- You can use it by simply using the name. Reference is implemented via pointer, a constant pointer with automatic indirection, the compiler will apply the dereferencing for you automatically.
- Think of references as an alias to existing object in memory.