Pointers are a very difficult and troublesome area for most C++ programmers, beginners and intermediate alike. Most questions pop up about their use and why we even need them. I hope this website helps answer these questions for you and demystify the C++ pointer.
To understand pointers we have to understand how variables are stored. Variables are stored in memory cells inside the computer's memory. The computer's memory is made up of consecutive memory cells, a byte long, each with a unique address.
But we are not going to think in those terms. We are going to believe that computer memory is made up of a bunch of houses on one very long street. Thus, each house is a memory cell. Now, there must be a way for us to find this house. Well, in each house someone lives there. This person of course has a name and this will be our variable identifier. For example:
This will put paul into a vacant house, of size int, somewhere along the street. We do not decide where paul will live. This is done by the operating system and the compiler at runtime. In a later section, we will discuss how to get paulto tell us where his house is.
Currently, paul does not have anything stored in his house. But, we all know that wouldn't be any fun to not store anything. So, each house can of course store a value. Continuing from above:
John = 34;
This will store the value 34 into paul's house.
One last thing before we move into other topics. Let us remember that paul's house is a unique number in memory. In addition, if paul's house was numbered 1234 we know that his house is between houses 1233 and 1235. This is a very important concept for later sections.
Every time you declare a variable, the compiler puts it somewhere, which you can now refer to as an address. Once you know that address, you can use it.
Like a reference, when you pass an argument to a function, the argument is passed using its address. This allows the calling function to dig into the address of the variable (the argument) and use the value directly. This transaction, like that of passing an argument by reference, allows the calling function to alter the real value of the argument. Using this ability, a pointer can allow you to return many values from a function; as opposed to a regular argument passing where the data, although changed inside of the calling function, will regain its previous value once the calling function is exited. Therefore, passing arguments as pointers allows a function to return many values, even if a function is declared as void.
When you declare an array, you must specify the dimension of the array. That's already a problem: what if you don't know and don't want to know the dimension of the array? Pointers provide an ability that regular arrays do not have. Since pointers have a different and better system of managing memory, a pointer can store an array of almost any size; this is tremendous when dealing with arrays of characters or a whole text. Using this feature, when declaring a pointer in replacement of an array, you do not have to worry about the size of the array, the compiler will take care of that. Again, this feature allows you to pass pointers to a function (just like arrays) and return a value that has been altered even if the function is declared as void. This is even more dynamic with multidimensional arrays.