Pointer arithmetic.
Here, a pointer points to memory and the type of the pointer determines the type of the data that can
be accessed through the pointer.
So here so for example, an integer type pointer will point to an integer in memory and you dereference
the pointer to get the integer.
So if the pointer allows it, you can change its value through pointer arithmetic.
So for example, you can increment or decrement the pointer.
So what happens to the value of the memory address depends on the type of pointer So assigns type.
The pointer points to a type.
Any pointer will change the pointer in units of the size of that type.
So if you increment the integer pointer, it will point to the next integer in memory and change in
the memory address depends on the size of the integer.
So this is equivalent to array indexing here.
So where an expression such as for example v one means you should start at the memory location of the
first item in the V and then move one item further in memory and return the item here.
So let's create an V variable here or let's create an just A here.
So we're going to assign it just an integer, but we will create an array for example, one, two,
three, four and five.
And here we're going to create a pointer, a here named point P A, and we'll assign this to a and I
will explain all this code here, of course, as I do always and here pointer array, the reference
point array and here 11 assign it 11 and here a one year 12.
And.
To.
To here equals assign it 13 and hear the reference.
Put the referencing operator here and PA plus three and assign it 14 here for example.
So let's run our code before of course, we have to write the print statements here.
So the first line here allocates an array of five integers.
On the stack and initializes the values of the number from from 1 to 5.
In this example we have an initialization list is used.
The compiler will create a space for the required number of items and the size of the array is not given.
So if you give the size of the array between the bracelets here, then the initialization list must
not have more items than the array size.
So if the list has fewer items, then the rest of the items in the array are initialized to the default
value, usually zero.
So the next line in this code here.
Obtains a pointer to the first items in the array.
So this lines after array items in various ways.
So the first of these a changes the first item in the lines, first item in the array by the referencing
the pointer and assigning it a value here.
The second here.
Um.
Yeah, let me hear.
The second year uses indexing, but this time with a pointer and assigns a value to the third value
in array.
So and the final example here, as you can see here, this expression uses a pointer arithmetic to determine
the address of the fourth item in the array.
So remember, the first item was index of zero here.
So and the reference is the pointer to assign the item value after this.
The array contains here.
Uh, here.
After.
After all of this here array contains 11, 12, 13, 14 and five.
So.
Now I want to illustrate this for more example.
So actually, let me create a diagram for it and I will show you what we did here.
Here.
This is an example of what we did here with this code here.
So basically this code, we will write it here.
Does this illustration as I diagram as I draw it here so you can watch and just illustrate it by your
own.
So if you have a memory buffer, which now we're going to.
Try the second example here with C plus plus pointers.
So if you have a memory buffer containing values in this in this example, I'll look at an array and
you want to multiply each value by three.
You can do using the pointer arithmetic.
So let's create an example here.
So make this comment.
For.
We will use the for loop here.
Integer or integer A.
Actually, let's create a new integer here.
Integer.
Camp here.
Or we can create an integer in this function integer.
Here, zero.
We will assign it first to zero.
And while it's less than five, then increment integer by one here plus plus E here.
And then we're going to use the pointer arithmetic to increase its value by one here.
So the loop statement is complicated and you will need to refer back to the operator precedence in this
lecture here.
So the postfix increment operator has the highest precedence.
The highest precedence is the reference operator here.
And finally this operator has the lowest of the three operators.
So the operations are run in order.
The first.
Then this.
And lastly this.
So the postfix operator returns the value before the increment.
So of the pointer is incremented.
So the next item in memory.
The expression uses the address before the increment.
So this address is the referenced which is assigned by the assignment operator that replaces the item
with the value multiplied by three.
So this illustrates an important dereference between pointers and array names.
So you can increment a pointer, but you cannot increment an array.
So let's so you can do this here, for example, P plus equals increment by one statement here.
And you can do this with pointers, but you cannot do this with arrays here.
One.
Cannot hear or error here.
As you can see here, you will get an error because invalid operands to binary expressions.
And actually you can of course use the indexing with this both on arrays and pointers here.
Let's test comment.
If you download this code in the lecture section and you can use it, of course, and you can play with
these pointers in your own time.