Hello, and welcome back. Now that we understand pointers a little bit more detail,
I want to talk about character pointers,
mainly how you can use character pointers to represent strings.
Strings are character arrays, multiple characters, words.
So we now know how arrays relate to pointers in the concept of pointer arithmetic.
Very, very useful concept.
These concepts can be very useful when applied to character arrays
strings because now we can easily traverse a string character by character.
One of the most common applications of using a pointer to an array
is as a pointer to a character string.
The reasons are one of notational convenience and efficiency.
So using a variable of type pointer to char
to reference a string gives you a lot of flexibility.
So you're going to try to use character pointers
as much as you can over character arrays,
specifically using character pointers to pass data to a function.
All sorts of nice things like that.
So let's look at an example that uses an array to copy a string.
In this example, our array parameter is going to be a character array.
You'll see that right in the function declaration.
I want to compare this to a function that takes a character pointer
and show you how the character pointer function is a lot more efficient.
So in this example, we have two functions,
one that takes an array and one that takes
pointers.
In the first function copy string,
we can go through the entire array using a for loop I equals zero
from sub I and we go all the way until we see the null character string
and then we increment our loop control variable.
And then all you have to do is say 2 sub I equals from sub I. We're doing a direct
assignment there.
And then at the very end after we've gone all the way until we see null character, we add
a null character to the end of our to array,
pretty basic. Now let's see how we do it when we use pointers.
First, we declare to pointers as the function declaration in the copy string
by just saying char star to char start from.
In our for loop, we don't have to actually
declare any variables because we have pointers
and we just want to iterate until the from hits the null terminator.
And then we can increment both the from and the to pointers.
Because here we're doing point arithmetic.
We're we're just adding one to the pointer which is increasing its address by one,
which is getting the next character.
And the next for loop line we just dereference
both to and the from because we want to assign
the actual value that the pointer is pointing to, the actual character
to the to pointer.
And then at the very end, we
add the null character to wherever the to pointer
is pointing to which would be at the end of the last address of the string for from
but now pointing in the to address.
It doesn't look too bad. I mean you have a for loop.
You basically have the same amount of lines of code.
But you see the difference in syntax.
You don't use subscripts and you use pointers, use the asterisk.
Again, really an issue of notational convenience.
You don't see a big advantage here.
But you can actually optimize this more to even get
down to less code when you use the pointers.
So if you have a char array as a pointer,
if you have an array of characters called text, you could similarly define a pointer to it
and use the elements in the text.
So here we could say char star text pointer,
that's how you define a character pointer.
If the text pointer is set pointing to the beginning of the array of chars called text,
then you can just increment it by one
using these pre and post implementation and decrementation operators.
So this above, this plus plus text pointer is going to set the text pointer
pointing to the next character in the text, which is text sub 1.
So if we want to look in the next ray just like for integers,
all we have to do is add 1 2 and we can get the next character.
It's much more useful though in character arrays because
you can easily manipulate characters one by one.
If you do a minus minus text pointer, which is the decrementation operator,
that will set the pointer to the previous character in the text
assuming that the text pointer was not pointing the beginning of the text.
Because if it was, you'll have these these under bounds errors.
If it was doing a plus plus to the end of the string, you'd have an out of bounds error.
You always have to be careful just like an integer arithmetic to not go past the bounds for an array.
But very simple, it relates very closely to when you do integer
pointer arithmetic.
Character pointer arithmetic is the same thing.
It's just even more useful because now you can actually look at individual characters without using a subscript.
So if we look at that previous example
and we can actually optimize it a bit more using pointers
to make it even less lines of code.
So now in our copy string, we do this function declaration is the same as before,
create a couple pointers using the asterisk.
But we don't use a for loop anymore because it was kind of pointless
to use a for loop in the previous example
because we didn't actually need to initialize the loop control variable.
All we needed to do was
exit a loop on a condition and then we are actually using the incrementation
and decrementation operators in the for loop, but we don't even need to do that.
It actually becomes even much simpler here because if we use a while loop,
all we have to do is look at the while loop and
it will jump out if it's pointing to a null character because the null character
is actually equal to zero.
And the while loop remember that Booleans in c
are represented by integers, one is true and then
zero is false or really zero is false and then anything else is true.
In this case, null character 0 which is false, it's going to jump out.
So all you have to do for the exit condition in the while loop is put an asterisk from.
What is that doing that's dereferencing from to see what the value of that character is.
So when we're to the end of the string that we want to copy from we jump out.
Otherwise we just actually put our
modification of our pointers right in there, two plus plus from plus plus.
Now this is a post incrementation operator.
So what this means is the plus plus is on the right hand side of the variable.
And when you do this, it means that
you don't add one to it until after the statement is done.
So what this is going to do, it's going to say star 2 equals start from.
And then after that statement is done, it's going to add to 2 1 and it's going to add to from 1.
If you had the plus plus before the variable name,
it would add to that variable before it executes that statement
and that would be a problem because we'd miss essentially the first element
being copied in the string.
But we don't need any more lines of code. We're doing an actual incrementation
pointer arithmetic and assignment in the same statement,
so we reduce the code by one.
Again, the notation is very simple.
We're doing an integer arithmetic assignment very easily.
And then at the end, after we're done, copying all the strings,
we just add to the end the null character
to pointer which we dereference
because the two is actually why it goes through the loop it'll be pointing to the last character.
So it's just a little cleaned up a bit here.
It shows you the power of pointers
and how you can use pointers and arithmetic in the same lines
and with these incrementation and decrementation operators, these plus plus and minus minuses,
it makes it even quicker and easier so it's more efficient as well.
If we wanted to use these copy string functions, we could just create two character arrays.
We can pass the character arrays directly to the copy string because the 0 character arrays are
pointers.
They are pointers, so we don't have to actually
de-reference them or add in addresses or anything. We could just say string2, string1.
And so what this is going to do is it's going to copy
a string to be copied to the string2 character array
and then print it out using printf.
I wanted to go through an example of how you can use character pointers
instead of character arrays and why they are sometimes preferred.
We mentioned that it's it's sometimes programmer preference
where if you've programmed in previous languages like
like Fortran, you may be used to raise. But if you have it,
you you'll see the benefits of using pointers.
Hope this was helpful. Thank you.