Her and pointers are actually related and even interchangeable in some situations.
So I had this example so it Gerais virus that's a blank that betray Ramus five elements and it's going
to put them to even numbers 10 that's
and said I had a pointer as well.
So that no point at the start then I'll do something interesting and I'll say the pointer is a signed
array.
That and if I output value pointed to pointer it's
your like that it's outputting the value of the value pointed to by pointer right.
That the reference operator in phi run this.
It says to is kind of interesting.
So notice that I didn't use the ampersand of this variable k it didn't do this.
And that is because an array is really really is kind of like a pointer in the first place.
So it really what this mind does is it says the pointer is assigned the first element pointed to by
Array k.
So it's equivalent to saying this is a address of the first element k.
These are the same I tend to prefer this because this is quite a bit more explicit and it makes quite
a bit more sense.
So let's actually see an example of this and the address element.
Zero is zero would be the first one.
This one is
so I could say this
and I could also say
it's one it is
the address of Element 1 that way.
And if it run that you'll see that these are identical except for the very last part is 1 0 for element
0.
OK.
And for Element 1 it's 4 and that is because it's 4 bytes offset so 4 bytes because an integer is 4
bytes and it's the next one after.
OK.
So this would be kind of this address and this one would be this address.
When using arrays I tend only to use the index square brackets like this instead of using the array
variable itself.
And so I could I could do this and get the same thing I had before.
So it's offset by 4 right.
It's the address of the next element.
But I tend not to do that because again I think this is a bit more understandable and readable even
though it's a bit more typing.
OK
so we can also use pointers just like arrays just like having this I could do something with this pointer
of 1 to 5.
And if I output
array one Hintz the one that
this should have changed because now the pointer is pointing to this element.
This one has the index at one so it's actually pointing to the second element.
So you should see five being output right here.
So I can I can actually use sort of the array syntax for pointers to.
So that's that's another similarity.
Pointers and arrays share.
So let's another example.
Get rid of all this.
That
in.
Say some equals zero.
A Randy the same example we did in the last lecture and or have pointer as well.
Or And we're going to send it to Erie.
So again this makes the grade point to the first element the three and which do this.
And this makes more sense to me and we'll loop through the whole thing.
Zero less and five
plus plus and we'll get some of them together.
But we're going to use the pointer syntax so that
some this some
can run this.
So some 30 K and notice we're using We're only pointing to the array we're not actually using their
aid directly but in fact it actually went through each one using the pointer and summed the value pointed
to by that pointer.
Each iteration they each index.
And there's another way we could do this could do it like this.
Plus plus that.
So you're in that we also get 30 and exact same thing.
So we're getting the value pointed to by pointer each time.
And then we're actually incrementing a pointer.
So the thing to understand about incrementing is that it will move the pointers.
What it's pointing to by the size of the type that it points to in but it's so it's pointing to an integer
array and integers for bytes so it actually will move the address for bytes.
So then will point to this one.
And then this one and then so on.
So in this example we're pointing to each element of the array using the pointer variable.
So in each iteration we point to the element in.
So for example this one is an int so it would move it by 4 bytes that say the array was a char type.
It would only move it by one byte each time.
So they aren't completely interchangeable though you can't actually increment an array for example.
OK that wouldn't work because you'd be changing the where that array points to.
So say I did this here eight plus plus K doesn't actually work
but it would actually be pointing here which is wrong.
Right.
You kind of want it to always stick to to this first element and you always want it to point to this
first element K because otherwise you'd be sort of offset and it would get confusing and you'd get confused
and bad things would happen.
OK.
So we'll see some more similarities in the next lecture.