Introduction
Think of a treasure-finding game, in which you find a map which tells you to go somewhere, where you find the treasure. The map could be called a "pointer" because it points you to where the treasure is. This is the basic principle behind pointers in C++.
Pointer
A pointer is in principle a variable like any other, but instead of any value, it holds an address in memory, where the value resides. This may seem unimportant for types such as int or float, but it's extremely important for using C-strings, arrays and for dynamic memory management. It's also the only way for functions to change variables outside of their scope1.
Use in code
To declare a pointer, you just need to add an asterisk( * ) right before the name of the variable declaration:
int *pointer; // this is a pointer int variable; // this isn't a pointer int variable, *pointer; // first isn't a pointer, while second is
C++ will complain fiercely if you try to assign a pointer of one type, say int*, to a pointer of another type, say float*. There are ways to bypass that, but you generally shouldn't2. Only exception to this is the void* pointer, because it's considered generic3.
Finding the address
In order to find the address of a variable, you need to prefix it with the reference operator( & )4:
int var = 123; int *ptrToVar = &var;
Getting the value
Now, to get the value pointed by a pointer, you need to prefix it with the dereference operator( * )5:
int newVar = *ptrToVar; // newVar will be 123 *ptrToVar = 321; // now var will be 321
Array usage
Last but not least, a pointer can be used as an array of variables of that particular type. The syntax is exactly the same as with arrays.
Point to nothing
Just as a treasure map could be trolling you and have no X, there is a way to make a pointer point to nowhere. This is achieved by making it zero:
int *ptr = 0;
with this in mind, I can check if my pointer has an address assigned to it, or if for example an error occurred. Keep in mind though, most systems6 will crash your program if you try to write to or read from that special address7. It is also the only situation where C++ allows you to assign an integer to a pointer of any type.
Pointer arithmetics
While you can add and subtract integers from a pointer, the result depends on the type that is being pointed. In general, the resulting address will be the integer times the size of the type in bytes plus the original address. In principle, this is done so as whenever you for example add 2 to a pointer, it'll move 2 elements forward, instead of 2 bytes forward. Remember that you simply can't do pointer arithmetics with void* pointers, because their type is unknown.
Reference
References act almost like common variables. The difference is that they must reference a common variable8. They're in a sense a short-hand for pointers, providing convenience. Also, very useful for passing parameters to functions by reference, instead of the default by value.
Use in code
To declare a reference, you need to add the ambersand( & ) right before the name of the variable declaration:
int &ref; // this is a reference int variable; // this isn't a reference int variable, &ref; // first isn't a reference, while second is
by directly using the reference, you're also altering/using the variable it references:
int var = 123; int &varRef = var; int newVar = varRef; // newVar is now 123 varRef = 321; // var is now 321
Conclusion
I guess that is all for now. Keep practicing, and don't forget that we're only scratching the surface here.
Happy coding ;-)
Next tutorial will be about Structs and Classes. Time to bring out the big guns and expose the true power-horse of C++!





