Probably the main thing about a pointer is that its value – as in, the location in memory being pointed at – can be directly manipulated, whether that is warranted or not. A user can write `(pointer+1)` to point at the memory slightly to the right of what it was originally pointing at. It’s treated as the next item in an array as though the pointer was to an array (either the first item, or really any item). Indeed, `*(pointer+x)` is nigh-identical to `pointer[x]`. This is potentially very useful, but also potentially very dangerous as there are not any real ways to validate if the memory is fair game. Not all invalid memory reads/writes crash a program (or a crash may be delayed). Of course, it could also be the NULL pointer indicating it doesn’t actually exist, giving you the option to point to nothing.
By contrast, a reference is sorta like a pointer where the compiler takes care of managing where it’s pointing for you. There is no ambiguity as to whether the variable pointed at is an array, or even if the object exists. A variable passed by reference must exist and you cannot insert a NULL value instead.
So like so many things in life, a pointer is potentially more powerful and expressive, but also more dangerous and there is no built-in safety net.
Latest Answers