I said earlier that are pointers and arrays almost work interchangeably.
Well that only applies to one dimensional or it is with pointers pointers with multi-dimensional arrays
is a little bit different than using pointers with one dimensional right.
So say we had this array A to the Marybel last time you this well you'd think you could do something
with this.
And unfortunately you can't.
So you think you could do this because there's you know one dimension here in one dimension and there
and maybe you know if you had one pointer and another pointer kind of in between here that would kind
of compensates for this one dimension the other dimension.
Well unfortunately this doesn't work right.
So an error you can't initialize that variable with the array.
So it's saying so how would we have a pointer to a 2D array.
Well we could do this instead.
When juror
this and have B 10 here is there this needs to be the same way.
And you could just do that k.
So what this is is a pointer to an array of 10 items.
It does exactly what you want.
So let's see an example for example.
So let's say we had constant max.
Call it 10 here.
Max
it's like that
and sleep over all the rows.
In all columns
in just say pointer are in.
King can do this because again sort of work like arrays at least in this dimension.
Right.
And of course we have an array of them so we can use.
See here.
This doesn't matter too much or it's a sign and some values and just output those values are and who
are.
C.
Good to see
you pointer of R and C.
So if you're in this should see actually work.
So are using that pointer to modify the to the array.
Right.
So you go through the first row and then we just give its values and the second Aronne its values right.
And so on and so you can do this as well.
Dynamic memory.
And in fact in a couple of different ways.
So the first way I'll show you is say we don't need this anymore and we can say this new and in Max
and x a.
So this will actually create a contiguous to the array in memory and has pointer point to it.
And just like normal we can do is delete this pointer.
So this is exactly the same and it works at the same see the same result.
Well let me show you one more way you could create a two tier and then is using an int pointer pointer
that I showed before.
And so now you're going to see why we couldn't do the end pointer pointer before.
So it's a pointer this and watch say new
winter or re k so this creates a new pointer and you're wondering like what kind of direction that R8
goes.
Well in this case you can kind of think of it as it goes sort of down.
So it's kind of this first column but it's the first column of pointer's.
So it's an array of pointers.
So then how we set all the arrays on it is this the loop or are equals zero AR's less than max are A-plus
plus.
And the pointer of our equals new int max.
So each of these pointers now will be assigned an array of this Max like
so now you can use it just like you did for an.
So you can
we help
can see in kind of use of like before
and but we haven't cleaned it up yet.
OK.
So notice that we had
so that we made kind of the sort of big column of integer pointer or like that array of integer pointers.
This line here.
And then we kind of made all the arrays after for each row.
Well we have to do that in reverse if you want to clean it up so we have to clean up all the rows first.
Yes.
So those leaks are
or and then we delete
the pointer K so that it does everything in reverse deletes all the rows first but we made all the rows
second.
And they made this pointer first and then we have to delete the pointer last.
So it kind of goes in reverse order to its creation.
So what is the difference between the two.
Well this what we have for ain't
for Star pointer.
Like that's new in x.
Max Right.
This one is contiguous in memory.
Right.
So in memory they actually are all next to each other.
Unlike this one which is possibly piecemeal in terms of the memory out because we don't know what addresses
the new operator will return to us.
They might be contiguous but maybe they're not.
So we don't know.
And when you use them.
Well if you want something pretty flexible in terms of the row and column sizes you should use the second
one this in your pointer method for that case.
But if you know the size or you can sort of calculate the size somehow then you should probably just
use this sort of contiguous one because this is usually more efficient.
OK.
So we'll talk more about pointer pointer and just pointers to pointers more in the next lecture.