- A pointer is a variable.
A variable that holds a memory location.
As a variable it's possible, although terrifying
to some, to place pointers into an array.
Why and how you would want to do such a thing
is this movies topic.
Here I'll show you how to set up an array of pointers.
To keep things simple, I'll use strings for the example.
You'll see how to access strings,
evaluate the strings, and convert them from
array notation into pointer notation.
A pointer array is simply a collection of memory addresses.
So, how do you get to the situation where you
need an array of addresses?
One of the best examples is to use an array of strings.
Here you see a pointer array.
It contains room for five memory locations.
Those locations are preset to the strings listed
in effect the declaration is for an array of strings,
although, that's not technically correct.
The strings are displayed by using a four-loop
at line 14 built and run this code
and each string is displayed.
To prove that the array is a clutch of memory locations,
modify the code.
In line 15, change the percent S to percent P.
Remember that percent P is the memory location
or address place holder.
Save the change, build and run.
Now, you see a series of memory locations,
because the compiler allocated space for the strings,
and stuck no characters on the end,
each memory location does reference a string.
By the way, this method of storing multiple strings
is far more efficient
than declaring a two-denominational array.
In that case, you must declare the array
so that each string occupy's as many characters
as storage as the longest string.
Here in this pointer array declaration,
that's not an issue.
Open exercise P array two.
This code is another version of the previous exercise.
Here the four-loop moves through each pointer in the array.
For each iteration, the pointer variable CPTR
is assigned to the base address of each string.
A while loop then turns through the strings
one character at a time, build and run this code.
The output is the same, although, the method of
printing each string was different.
It's actually kind of crazy.
Pointer variable CPTR is used to display each character
by walking through the memory locations,
where the characters are stored.
Now, something bothers me about this code.
Yes, it's line 17.
If you haven't picked it up all ready,
it uses array notation.
That was very cowardly of me to do so.
A better solutions would be to
convert this heinous notation into pointer notation.
To do so, make this change to the code.
This notation uses the memory addresses stored in
the gain variable,
which you saw displayed in an earlier exercise.
So, the memory locations are used not the string.
Then the value X is added to each,
and each time the loop references the next array element,
which is the base at rest of a string.
It has to be enclosed in parenthesis,
because the memory location is manipulated first,
and then the contents of that location.
The asterisk fetches the string is referenced later.
Save this change, build and run.
The output is the same.
More can be done with pointers here,
and I could broach upon the topic of pointers to pointers
and all that, double-asterisk notation,
but that's more of a topic for an advanced course.
In fact, here's a tip.
When you see the dreaded double-asterisk pointer notation,
it's almost always a sign
that you're dealing with an array of strings,
or specifically, an array of memory locations
for each of those strings.