As you learned earlier, there is an interesting a very interesting fact about arrays.
So we can access the arrays element using a pointer.
So as you may know, a pointer is a variable that holds the address.
Instead of value.
So and since we discussed earlier that each element in the array has its own address, we can access
each race element using its address.
So to use a pointer as an array, we need to initialize to it to hold an array as shown here.
So let's we're going to change it our this, my array and we will write this my for example my oxalate
array here.
Or let's actually write it from zero.
So, as you know, we previously we manipulated some of the things here we did.
And actually let's clear the main method.
So we want to use it.
We will use it anywhere and we will also clear this that we changed it.
So we changed index two and index three.
So in place of the index ten and as you can see it here, so in place of the index three, we will write
the 87 index.
Two, we will write 8787.
And 69.
69.
Here.
So now we're going to create our array here.
So let's actually comment this.
Comments and we will create a new integer pointer array.
So integer, Peter.
Peter new integer.
This is going to be five size of integer.
So one, two, three, four, five.
Size of five, not the index five.
So if you have five, uh, size of array, then we have the from zero.
This means our maximum index is four by default.
So here now we're going to create this 25, 2148
827 30 Uh, no.
40 2148 827.
The 69 and 35.
So this is an initialization.
We have a pointer named Peter that points to the first element of an array containing five elements
here.
So.
However, the variable holds the first arrays element address at START.
So to access the next address, we can increment Peter variables.
So.
Peter plus plus we can increment the Peter variable, um, so that it will point to the next element
here.
So it will point to next element.
Actually, yeah, we can increment this and so it will point to the next element and next element here.
So we can use the wildcard symbol before the pointer name.
And as you can see here.
So.
Let's first actually create some.
Here.
Here we will create a print this variable C out.
See out.
PDR, PDR and inline.
And thus we will print this without the disk.
Without this, the wild card symbol.
So.
PDR and end line.
End line here.
So the former statement in this code will print out the value that pointer points to.
So let's actually firstly compile it.
CLS and G++.
Main.cpp.
Edit And as you can see here, this is our pointer and this is our first element, as you can see here.
So as we said earlier, the first this.
So let's here.
So this symbol p t r points to our 21.
So the first element so equals the first.
Element.
However, if we write it like that.
The P here.
Peter.
The Peter is just shows the entire address the address of in our memory that.
Our array lives on it.
So in this case it's going to print the address.
So at.
Rest.
Of array.
So this prints out the address of arrays, the second code line.
And here this prints the first element, which is 21.
So here we can access the value of each element.
And the address as well as its index.
So let's actually create some another example.
Here.
Let's, uh.
Here.
Clear that.
Here.
Clear that.
Here now, we will create something like that.
So now we're going to create.
Uh, the.
Some code that we can access the value of each element and the address as well as the index.
So the key out.
Oops.
The sea out.
See out the r e here.
E in this case, for example, to let's take E as a two and we will end line here.
End line and see out tr.
Again, he is a to here for example just.
He also can be three, 4 or 0.
But in this case it's going to print the address of our array here.
So now let's actually go to CMD compiler application and run it.
Here.
So as you can see here, this prints.
So in this case, our E was the E was equal to two.
So we've written the two.
Between these braces in the two code code lines.
So.
This is our.
P t r, and this is our regular p r, which showed us the same result.
If we use if you use that the.
Index.
We specified index.
It will get us an same result here.
So in next lecture we will create some code that we will initialize pointer, initialize the array length
and we will also display each element by incrementing the pointer and we will move forward the pointer
five times.
So I'm waiting you in the next lecture.