Hello, and welcome back.
Because point arithmetic is kind of a difficult concept to understand and grasp,
I wanted to go through one last example
that really demonstrates this concept, it does a good job.
And if you understand this example,
you're going to have no problem with point arithmetic.
So the first example that I want to show you is
the effect of adding an integer value to a pointer.
So when we do pointer arithmetic with addition,
we can actually see how it works in terms of the addresses.
So here in this program, all I'm doing is creating a string,
a character array, it's called a string.
And then I'm creating a character pointer called p.
I'm going through all the entire length of the string
which is multiple. So I'm going through the entire array.
Just name basic for loops strength length of multiple size and multiple.
And then I'm printing out each
actual string or character inside that array.
And also what I'm doing that I'm displaying the address
and then the address
with the actual number the I added to it.
So let's look at just the output of this because the output demonstrates
a really good example of point arithmetic.
So let's go to code blocks and run this.
So here we are in code blocks. I cleaned up the code a little bit.
I use str length, set a strn length because it's extra include.
And I want to just run this.
So when I run this, what you'll notice is, it's showing you how
remember what memory looks like, and it's showing you the values
doing the point arithmetic. So if you say multiple sub-0,
remember, multiple just contains a string.
But if we look at this and we say look at the array contents,
multiples of 0 is a, multiples of 1 is a space and then the rest are string.
That's the array.
Right next to this is the same as adding 0 to the pointer
and dereferencing it, adding 1 and adding 2.
And it shows you how you can do pointer arithmetic by
dereferencing the pointer itself and adding numbers to it.
So the pointer plus seven dereferencing that you get g.
Then in the third column, I'm showing you the actual addresses as
as we increment. So the addresses of each element
in the array using the ampersand just on the character array,
we see the address 060feff for element 0 and so forth.
And then we look at the addresses if we just add
to the pointer itself without dereferencing it,
and you'll see the addresses line up.
And so then we can look at the syntax to see what's going on here in terms of
the first thing that we actually display is the index in the array which is just a counter.
And then we display a percent c
which is the character at the array.
The next thing we're displaying is
dereferencing the pointer plus I.
So here what we're doing is we're displaying I
and then we're displaying the dereference of pointer plus I,
the actual value.
So we display
as the d
just the actual counter and then point c
is our dereferencing. So p is our pointer.
We're just saying p plus I, pointer arithmetic,
and then we're dereferencing it.
And then to get the addresses,
all we need to do for to get the array address is say
ampersand multiple and then the index.
To get the first get the address of
the pointer
here we just say p plus I
to get the actual counter which is showing you
how we're incrementing that p, we just do a p plus percent d.
But it does a good job of demonstrating
the various ways that the memory
that we're pointing to the addresses
are actually displayed and how they're equivalent
to the different indexes in an array and the actual pointers.
I thought this did a good job.
I want to look at one more example that does another good job
concerning pointer arithmetic with longs. Because here
these are integers. So all we have to do is add one and automatically
goes up to the next byte and so forth. Let's look at what we do, we do longs.
So in this example, I wanted to also just demonstrate how you can do the same
thing with a long and a long pointer.
With the character pointer, it was just a string character array.
But even for long arrays, which are different sizes,
we can still iterate through them using a pointer
and you can see how the number of bytes actually changes.
So even though we say we have a long pointer and we add one to it, we're actually adding 4 bytes to it
because a long is a little bit bigger and we'll see that in this example.
So now let's run this example in code blocks.
So here's our example. Again, we have an integer counter, a loop counter.
We have a long array 15l.
We're specifying l by putting the suffix on then we specify their l's,
their longs we have a long pointer that's pointing to multiple.
Again, because it's an array we don't have to use the address of operator.
We're going to go through the entire long. Here we can't use stir length we have to use size of,
and we actually have to divide it by each spot in the array.
So the total size divided by the single spot in the array,
that's when we want to jump out because
these remember our addresses that are incrementing by 4 bytes each.
And again what we're doing here is we're just displaying both
the pointer versus the array and showing you how they're related
and then we're displaying the actual total number of bytes
that the long is on our system.
And so this again just demonstrates how arrays
are related to pointers.
So if we run this,
we're going to see similar output. But what's a little different here is the address size,
going up by 4 bytes. So we're going to print out the address and the pointer.
So for the address and the pointer plus 0,
that's going to be equal to the array sub 0
which is address 6356728.
And to dereference the pointer, we'll get the value of 15.
So p plus 0 is still going to be equal to the array sub 0.
And then again we do the same for p plus one p plus 2 and p plus 3,
all equivalent to the correct spots in the array for the index.
The addresses are going up by 4 bytes, but we can still just add one 2 and 3
and we see that the values are the same.
And then it tells us this long occupies 4 bytes and that's why the addresses are going up by 4.
What's significant here is we can still add one to it
and the compiler knows to go to the next address.
So whether we have a long or a character or whatever that
particular data type is inside of the system,
adding one or 2 is going to add the correct number of bytes.
So just another cool example I wanted to show you
because this pointer arithmetic is really important and it's really useful
when iterating or traversing
an array, it makes much cleaner and much more efficient.
So I hope this helped. Thank you.