• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# multi-dimensional arrays

This topic is 5897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know that for doing a 2 dimensional array using a regular array you would create it like so:

int * Array = new int[Width * Height];
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
Array[x + y * Width] = 5;
}
}
...
delete [] Array;

but how would it work with a 3 dimensional array? im asking because i''m attempting to re-write my level editor to support multiple layers. Thanks in advance!

##### Share on other sites

  int * Array = new int[Width * Height * Depth];for (int z = 0; z < Depth; ++z)  for (int y = 0; y < Height; ++y)    for (int x = 0; x < Width; ++x)      Array[x + y * Width + z * Width * Height] = 5;...delete [] Array;

##### Share on other sites
int ***thing;thing = (int***) new int[whatever];for(int x = 0; x < whatever; x++){   thing[x] = (int**) new int[whocares];   for(int y = 0; y < whocares; y++)   {      thing[x][y] = (int*) new int[huh];   }}// now you have thing[whatever][whocares][huh]

[edit: ah, i just noticed that you wanted to do it single bracket
style.. oh well, i'll leave this here just in case anybody else
wanted it ]

-eldee
;another space monkey;
[ Forced Evolution Studios ]

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

[edited by - eldee on November 30, 2002 7:02:19 PM]

##### Share on other sites
Eldee, you should do it like this instead:
int ***thing;thing = new int**[whatever];for(int x = 0; x < whatever; x++){   thing[x] = new int*[whocares];   for(int y = 0; y < whocares; y++)   {      thing[x][y] = new int[huh];   }}// now you have thing[whatever][whocares][huh]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 11
• 9
• 9
• 40
• ### Forum Statistics

• Total Topics
634130
• Total Posts
3015705
×