# 2d arrays quickie

• 01-19-2002
gordy
2d arrays quickie
(checking myself here) when you declare a 2d array, array[x][y], is an array created that has x-1 elements 'along' and y 'down' or are both 0 based hence an array[3][2] will be accesible in the range array[0 thru 2][0 thru 1].

thanks,
• 01-19-2002
Salem
> or are both 0 based hence an array[3][2] will be accesible in the range array[0 thru 2][0 thru 1].
Yes, like this
• 01-19-2002
gordy
crikey, you wasted no time :) thanks.
• 01-19-2002
minime6696
Suggestion...
A '2d' Array can come in many shapes sizes and forms. That is one of the most ineficient ones...

The 'best' way to do this is to use a pointer and a little math:

blah *myarray = new blah[sizex*sizey];

myarray[(y*sizex)+x] = wutever;

Hope that helps :) .

SPH :rolleyes:
• 01-19-2002
minime6696
Another thing...
I also suggest instead of simple arrays using a linked list... but that is far more to talk about... if interested reply or email!

SPH :confused:
• 01-19-2002
sean
Another question, does array[x][y] create x arrays of y, or y arrays of x?
• 01-19-2002
Sorensen
>That is one of the most ineficient ones...

Why? Dynamic allocation wasn't mentioned in the original or subsequent posts (until yours). Stack allocation will be quicker.

> Another question, does array[x][y] create x arrays of y, or y arrays of x?

x arrays of y.
• 01-20-2002
Salem
> The 'best' way to do this is to use a pointer and a little math:
Which is exactly what the compiler uses when you write [x][y] anyway, so what's your point?
• 01-20-2002
minime6696
lol....
Once you've done a few projects... you will find the [][] sort of static pointer will not work as well as simply doing it yourself...

Don't make bad habbits, this very thing almost wreked one of my projects:

Try dynamic memory allocation here:

char **blah = (char**)new char[x];
*blah = new char[y];

blah[1][1] = (char)6;

you will usually need to set it up for a 'yx' call instead of 'xy', its jsut not a good habbit to 'form'.

SPH
• 01-20-2002
Sorensen
>you will find the [][] sort of static pointer will not work as well as simply doing it yourself...

Why?

>Don't make bad habbits

Thanks for the advice.
• 01-20-2002
itld
confused???
howdy,
i'm very confused???
i am writung an app that will include 3 seperate arrays of about 200 objects each. which form do you suggest. pointers and math, linked list or dynamic memory allocation??
the last 2 i have no idea how to do...oh well back to the books.

M.R.