Second, what do they do. Well, I might as well address
this issue as well as what they are at the same time. Pointers are what they
sound like...pointers. They point to locations in memory. Picture this: a big
jar that holds one thing, the name of another jar. In the other jar is the value
of an integer. The jars are memory locations. The jar that holds the name of the
other jar is a pointer. It points to the other drawer.
How can you use this? Well,
look at this little piece of code:
Guess what! The cout outputs
the value in x. Why is that? Well, look at the code. The integer is called x.
Then a pointer to an integer is defined as pointer. The astrick(*) symbol means
that it is a pointer. Then I give the memory location of x to pointer by using
the ampersand(&) symbol. It gives the memory location of the variable it is
in front of. For example, if the jar that had an integer had a ampersand in it
it would output its name, or location. Then the user inputs the value for
x. Then the cout uses the * to put the value stored in the memory location of
pointer. Huh? Picture the jars again. If the jar with the name of the other jar
in it had a * in front of it it would give the value stored in the jar with the
same name as the one in the jar with the name. It's not too hard, the * gives
the value in the location. The unastricked gives the memory location.
I hope this has been at least
an interesting introduction to pointers. I do not suggest that you play around
with them too much as you can do unpleasant things on your computer, but you now
should have a better understand of what they are.