Hello and welcome back in this lecture I want to provide a demonstration of our challenge with pointer
arithmetic and using the cost of modifier spring up code blocks and create a new project we're going
to name this one pointer arithmetic example.
OK so what we're going to do here is we're going to actually determine the length of a string by using
pointer arithmetic.
So the first thing you want to do is I want to create my function declaration.
This is going to return the length of the string so it's going to have an int as a return type when
to call a string length.
And this is just going to take a pointer but it's going to be a constant pointer we don't want to change
the value of what's pointing to.
So that means we would use the Consta as the first keyword and then we would specify the type which
is a character and we would say a star and we can name it whatever we want.
String.
Now again just to reiterate if we were to put that constant here instead this would say that the address
of the pointer the pointer's value cannot change.
But again it's kind of redundant and it doesn't really need to be done because we're doing pass by value
passed by copy.
So you have a copy the address anyway so it doesn't matter if you change it in there but it is important
to change what you're pointing to if you do not want the value of what you're pointing to to change.
You definitely want to do constant here and that's important.
So now all we have to do is we're going to create our main our main or we're going to do in our main
as we're going to call string length string length function with different strings.
So let's pray now and remember it returns energy so we can say percent D and we'll just going to print
out the length of a couple of strings that we pass.
So I want to call string length and we're just going to pass in some constant strings so we can just
put things in double quotes here.
We don't even have to create pointers.
You could create a pointer and assign it and then pass it in.
But you just have to make sure it's a constant.
But since it's easier and less code we can just say string length test.
Again that's a constant literal string right.
We'll test it with some more we'll say put a space after it.
Let's actually Instead let's let some line feeds here recall string length and actually let's call with
empty empty string.
Make sure return 0.
Then our third one will say string length.
We'll just say Jason.
Now incidentally when you create these pass the string length things they automatically get the null
terminator added to them.
Right.
We learn that when we declare pointers.
That's one of the advantages if you're going to create a pointer and it's going to be a literal.
You don't want to specify the size you know you'd want to do something like character.
If it's an array because an array is a raise a pointer.
But if we we want to want to specify the size we could say character P and then inside here we could
just say Jason All right.
Remember that lesson where if we don't specify the x size for character raise it will automatically
add a null terminator and create the size for you.
That way you don't make any mistakes if you put the size inside the array.
You might not have enough of the null terminator.
That's the same thing when we pass the literal to the stirringly the null termine or gets added to the
end of it.
So we could have created these variables you know and honestly and done something like this and then
passed in each one.
But I didn't want to waste time creating variables.
So I'm just going to do that.
Now what's really important for this code is the function.
So we're going to create our string length.
Remember again it returns an integer and it's going to be a constant.
So we can just say constant char Star story.
And you also notice that the function declaration variable name doesn't have to match.
I could put this in strings and that's fine as well.
Now inside this function we have to create a local pointer because the local pointer is going to point
to the end of the string and then once we have the address for the end of the string we can minus the
address for the beginning of the string the address of the beginning a string is what's passed in string
is all magically going to be pointing to the first element in the array or the first address of the
array.
So if we create a local pointer and we'll just want to again create another constant constant char star
and we'll just say the last address bar.
And this is going to be equal to the beginning address of string and then we're going to incremented
each time in our loop.
So now we have last adder's again this is a local variable that's going to store the address right before
the null terminator.
So now we can write our while loop and our while loop is going to X when we see the null terminator.
Now we could do this today for ways we could say we could compare characters so we could say something
like.
While Star last address because we have to dereferenced it is not equal to
backslash 0.
Could do something like that.
But that's kind of a lot of code.
We all we really need to do is just to say while star at last address because if you dereference a null
terminator it's can be equal to zero and zero is equal to false right.
We know that booleans have 1 is true zeros false.
So if it's false it's going to jump out.
All you need to do is inside your while condition is put Star last address.
Again this is going to dereference the pointer and once it's pointing to the last address it's going
to be pointing to the null terminator.
Now we have to make sure that we modify the last address so that eventually points to the null terminator.
Here all we can do is we can say plus plus last the address.
So each iteration of the while loop is going to increment the address until it sees a zero zero represents
the null terminator.
So the end result of after this while loop is done executing last address is going to be pointing to
the address of the null terminator.
We can then subtract the address of the null terminator from the address that was passed in our string
and that's going to give us our length.
So that's all we have to do on the next line.
We can just say returned our last address and we actually want to minus the address so we don't need
to dereference anything or use the indirection operator last address string right.
So string is passed in we don't modify it at all that's pointing to the first character that will be
pointing to J.
That would be the address or for the first time we call line 8.
That would be the address of s last the address is going to be pointing to the address of this last
T.
Actually one more than that the null terminator.
If we say last address minus as the address of ass it's going to give us the length and that's what
we're going to return.
So I hope that makes sense again on line 17 recruiting a local variable because we need to keep track
of the last address the address of the null terminator.
After this while loop is done running last address is going to point to after the T the null terminator.
Then we can just subtract the last address from the string the strings address is pointing to s is when
it comes in and we don't modify that.
So let's see what this.
If this gives back the correct length when we run this we should see the length for those strings we
passed in.
So we see 17 0 and 5 let's see as 1 2 3 4 5 6 7 8 9 10 11 12 13 you got to count the space 14 15 16
17.
That's right.
We put in double quotes which is empty.
So there should be zero.
And then Jason should be 5.
So 17 0 on 5.
That's correct.
So what we did here was we used pointer arithmetic to get the last address plus plus the incrementation
operator and then pointer arithmetic to get the length by minus minuses last address minus a string.
Pretty neat.
It tries on trying to demonstrate the power of pointer arithmetic the notational convenience of it and
how you can easily increment it to get the various spots in a character array and get the characters
out.
In this case we're just getting the addresses and then use the addresses up to minus each other to get
the length.
So I hope this was helpful and I hope make sure you understand everything I just mentioned.
Again the concept of a constant the concept of plus plus the concept of the condition if you dereference
an address and that's pointing to an alternator is a zero.
So it jumps out then and then this concept of mine missing two addresses.
Thank you.