Public Group

#### Archived

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

# multidimensional arrays

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

## Recommended Posts

How do I make multidimensioned arrays? I know how to do 1 dimension
  //make an array that is array[sizex][sizey] int* array = new int[sizex]; //now how make the sizey part? //and when done, delete delete [] array; 

##### Share on other sites
A 3 dimensional array.
  int* array = new int[sizex][sizey][sizez];

,Jay

##### Share on other sites
Although thats a pointer to a 3D array.

  int Array[x][y][z];

is how to declare an actual array.

,Jay

##### Share on other sites
quote:
Original post by Jason Zelos
A 3 dimensional array.

  int* array = new int[sizex][sizey][sizez];

You might like to consider checking this code.

--

It is against the law to stare at the mayor of Paris.

Edited by - SabreMan on February 25, 2002 3:54:00 PM

##### Share on other sites
SabreMan, do you know how to do it?

##### Share on other sites
you could

  // the linear 2d arrayint *array = new int[sizex*sizey];// access by doingarray[x+y*sizex];delete [] array;//// for a planar multidimensional array// int **array = (int**)(new int[sizey]);for(i=0; i

saberman, how would you do such a thing, since you obviously know (by your comment about the code being wrong). since either one is a mulitdimensional array. there may be a better way to allocate planar arrays in c++, i am not sure since i rarly use planar arrays and tend to use c methods of allocation (except for objects).

##### Share on other sites
I knew the syntax was illegal.

Dynamically allocating multi-dimensional arrays actually takes quite a lot of explanation, so I'm going to defer to someone who's already written up a perfectly good explanation, Marshall Cline...

How do I allocate multidimensional arrays using new?

Pointers and arrays are evil!

Edited by - SabreMan on February 26, 2002 7:14:36 AM

##### Share on other sites
So, is this right?
  vector< vector > array; Because I want to use a multidimensioned container of tiles struct tile{int id;int walk;}; But don't understand how. If the above is right I think I would do for(i = 0; i < map_width; i++) { for(j = 0; j < map_height; j++) { //initialize each tile so I have an //array of tiles WIDTHxHEIGHT tile tmp; tmp.id = 0; tmp.walk = 1; map[i].push_back(tmp); }} 

Do you have any links where I can learn about STL? I don't know anything about it. [/source]

Edited by - evilclown on February 26, 2002 3:33:06 PM

##### Share on other sites
Something like this...
  int ***number_cube = new (int**)[5];for(int i = 0; i < 5; ++i) { number_cube[i] = new (int*)[5]; for(int j = 0; j < 5; ++j) { number_cube[i][j] = new int[5]; }}...for(int i = 0; i < 5; ++i) { for(int j = 0; j < 5; ++j) { delete[] number_cube[i][j]; } delete[] number_cube[i];}delete[] number_cube; 

##### Share on other sites
I''d do it like this using C style syntax.

  struct tile { int id; int walk;}; tile TileMap[map_width][map_height];for(i = 0; i < map_width; i++) { for(j = 0; j < map_height; j++) { TileMap[i][j].id = 0; TileMap[i][j].walk = 1; }} `

I think this is a more conventional way of doing it, if not easier to think about.

I''m going to have to talk to my eyes about reading posts all the way through before replying .

,Jay

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011318
• ### Who's Online (See full list)

There are no registered users currently online

×