Ok guys...
Prog Course 101
Today: The Multi dimentional Array
If you are using MSVC (which I'm assuming you are), the following structures are both the same:
int Toto[100];int Popo[10][10];
Why is that? Because the compiler understand what you are trying to do, and convert this:
Popo[2][3]=1;
into this:
Popo[2*10+3]=1;
Now that's logical... Totally logical. No more logic than that.
Thanks.
Now we will change a bit our course and go over DYNAMIC ARRAYS.
Prog Course 101
Today: The Multi dimentional dynamic Array
Say we declare those variables:
int *Titi = new int[100];int **Pipi = new int*[10];
We have Titi who is the exact same as Toto (there are differences because the compiler will faster Toto, but that's out of the point). But what is Pipi?
By definition, Pipi is a pointer to a pointer of integer. It means that when you allocate a
new int*[10]
you allocate an array of pointer. Logical, isn't it?
Ok now what you have to do with Pipi is allocate each pointers to arrays... with this:
for(int j=0; j < 10; j++) Pipi[j] = new int[10];
And from now on we can access to Pipi the same way as Popo.
But if you think Pipi is the same as Popo, you are totally wrong. The compiler, super-intelligent form of pure knowledge, will act differently with Pipi and Popo... In fact, Popo will be a single pointer and Pipi will be an array of pointers, which means that
Pipi[2][3]=1
will be transformed into:
*(*(Pipi+2)+3)=1;
instead of:
*(Titi+2*10+3)=1;
Which means WHAT? In assembly language, Popo will be much faster than Pipi... and Toto and Titi will be more or less equals (that depends of the situations in which it is used... The compiler may want to tranform Titi into an array of pointers of arrays...)
(( Man I love those variables name...
))
Am I right?
Edited by - Poltras on June 19, 2000 5:34:26 PM