Hello and welcome back. We have another challenge for you.
In this challenge, I want you to learn all about pointer arithmetic.
Remember, pointer arithmetic is a really neat concept.
It's a notational concept.
But it really illustrates how
arrays are related to pointers. You can traverse an
array very easily with a very simple syntax that reduces your line of code
and it's really neat concept.
So you're going to learn all about pointer arithmetic.
We're also going to learn more about the constant modifier.
So in this challenge, I want you to write a program
that tests your understanding of pointer arithmetic and the const modifier.
We've learned about both of these concepts in previous lectures.
So I want you to write a function that calculates the length of the string.
The requirements are the function should take as a parameter constant char pointer.
A constant char pointer means that you cannot change the value
of what the pointer is pointing to,
and you're not going to do that with the length.
The function can only determine the length of the string using pointer arithmetic.
I do not want you to use the str length function. I want you to use pointer arithmetic.
And the way that I'm envisioning this function to be implemented is that
you're going to get the address of the end of the pointer.
That would be the length of the pointer essentially.
But the way that you can get the address of the last string and the pointer is you can
write a while loop.
The while loop can go all the way to the null terminator character.
Or the null terminator character, remember,
that's represented as int.
So the null terminator is actually a zero. So you could write a while loop
and all the while loop has to include in its condition
is dereferencing the pointer.
Because if the value of the d reference pointer is zero, jump out of the loop
and that would be the null terminator.
So I want you to go through
the entire pointer.
And while you're going through it,
keep incrementing that pointer.
So now you have a pointer that's pointing to the end of the string.
Then to actually calculate the length,
all you have to do is minus two pointers.
The pointer that was passed in
which is the beginning of the string
and then the pointer that you created in the function which is pointing to the end of the string.
If you minus the end of the string pointer
from the beginning of the string point, you'll have the length of the string.
So you'll be doing pointer arithmetic by incrementing
the pointer one by one to get to the ending address and then you'll be using point arithmetic
to subtract each pointer.
This is really probably the the most valid way to use
point arithmetic for subtraction. You usually don't add two pointers
because why would you add two addresses.
You may subtract two addresses to get how far away
two elements are in an array,
that would make sense, but adding will not. So just to reiterate,
the function can only determine the length of the string using point arithmetic.
Create a local pointer in your function,
increment that pointer each time to get to the end
until you see null terminator, a simple while loop.
And then once you have the ending address,
you already have the beginning address, subtract those two pointers that will give you the length.
You should return that length as an int from the function.
And then you can just print out the actual length itself.
So this assignment is getting you familiar with point arithmetic
and also how to use the constant modifier because you don't want to change the
actual value that it's pointing to
and subtracting two pointers as well and also using a while loop
where you can look at the pointer's value itself
to determine the null character. Good luck.
I'll provide a solution in the next lecture. Thank you.