Hello, and welcome back
In this lecture, I want to provide a demonstration of
our challenge for pointer arithmetic and using the constant modifier.
Let's bring up code blocks. I'm going to create a new project. We're going to name this one
pointer arithmetic example.
So what we're going to do here is we're going to actually
determine the length of a string by using point arithmetic.
So the first thing I 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 int
as a return type. I'm going to call it 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 constant as the first keyword,
and then we would specify the type, which is a character.
And we would say star, and we can name it whatever we want, string.
Again just to reiterate, if we would have put the 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 pass 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 you're pointing to. If you do not
want the value of what you're pointing to change you definitely want to do constantly here,
and that's important.
So now all we have to do is we're going to create our main.
create our main. All we're going to do in our main
is we're going to call string length,
a string length function with different strings.
So let's print out, and remember, it returns a integer so we can say percent d
and we're just going to print out the length of a couple strings that we pass.
So we're going 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 in less code, we can just say string length test.
Again, that's a constant literal string.
We'll test it with some more.
We'll put a space after it. Let's add some line feeds.
Here we'll call string length. And actually let's call it with the empty string,
make sure we turn zero. And then our third one we'll
say string length,
we'll just say jason.
Now incidentally, when you create these and pass these string length links,
they automatically get the null terminator added to them.
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 a literal,
you don't want to specify the size.
You'd want to do something like character
if it's an array because an array is array is a pointer. But if we
wouldn't want to specify the size, we could say character p
and then inside here we could just say jason.
Remember that lesson where if we don't specify the size for character arrays,
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.
It's the same thing when we pass the literal to the string length,
the null terminator gets added to the end of it.
So we could have created these variables
initially 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 string.
And you also notice that the function declaration
variable name doesn't have to match. I could put this as 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 for the beginning of string is what's passed in.
String is automatically 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 char star
and we'll just say last address how about.
And this is going to be equal to the beginning address of string,
and then we're going to increment it each time in our loop.
So now we have last address. 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 exit when we see the null terminator.
Now we could do this two different ways.
We could compare characters. So we could say something like
while star last address because we have to de-reference it
is not equal to
backslash zero could do something like that,
but that's kind of a lot of code. All we really need to do
is just to say
while star at last address. Because if you
dereference a null terminator,
it's going to be equal to zero, and zero is equal to false.
We know that Booleans have one is true zero is false.
So if it's false, it's going to jump out. So 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 last address, so that eventually points to the null terminator.
Here all we can do is we can say plus plus last address.
So each iteration of this while loop is going to increment the address
until it sees a zero.
A 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 return
our last address
and we actually want to minus the address,
so we don't need to de-reference anything or use the indirection operator,
last address minus string. So string is passed in,
we don't modify it at all.
That's pointing to the first character. That would be pointing to j.
That would be the address or for the first time we call in line eight, that would be the address of s.
Last 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 the address of s, 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, we're creating 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 string's address is pointing to s when it comes in we don't modify that.
So let's see what if this gives us 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 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 that should be zero
and then Jason should be 5 so 17 0 and 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 point our arithmetic to get the length by minusing the last address minus the string.
Pretty neat. I'm trying to demonstrate the power of
point arithmetic, the notational convenience of it.
How you can easily increment, decrement
to get to various spots in a character array
and get the characters out. In this case, we're just getting the addresses and then using the addresses
to minus each other to get the length.
So I hope this was helpful. 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 a null terminator it's a zero so it jumps out then,
and then this concept of minusing two addresses. Thank you.