>> DAVID J. MALAN: Let's write a program that prompts the user for a string and
then proceed to print that string character for character one per line.
Now in the past, we would have done so probably with square bracket notation,
effectively treating a string is an array of characters.
>> But this time, let's instead treat a string for what it really is, a
pointer or an address.
Specifically, the address of a character, really the address of the
first character, in a sequence of characters that we collectively know
as a string.
>> Let's first declare a string for what it really is, char*.
And we'll call it s.
And then assign it the return value of get string.
>> Let's next do some error checking.
If s is null, let's immediately return so that we don't accidentally
dereference that null pointer.
>> Next, let's iterate over the characters in s as follows.
For int, i gets 0.
n equals the string length of s.
Do this so long as i is less than n.
And on each iteration, increment i.
>> And what do we want to do on each iteration?
Let's now print out on each iteration a single character
followed by a new line.
Well, what character do we want to print?
I propose that we go to the address that equals the sum of s plus i.
>> Now, why that expression?
Well, recall that stored in s is the address of the first character
in our string, s.
Meanwhile, i is being incremented on each iteration so that it starts at 0,
then goes to 1, then goes to 2.
>> So in other words, s plus i effectively represents the address of
the i-th character in s.
So if we go to that address by way of the * operator, we'll be going to the
i-th character in the string.
And that's the value that will be substituted for our placeholder,
percent C.
>> Let's confirm as much.
Let's save, compile, and run this program.
Make pointers, dot slash pointers.
And now I'll give it a string like hello.
Enter.
>> And indeed, I see H-E-L-L-O, with each char on its own line.