• Advertisement

Archived

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

multi-dimensional arrays

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

  
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 this post


Link to post
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 ]


::evolve::

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 this post


Link to post
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]


Share this post


Link to post
Share on other sites

  • Advertisement