# Thread: Multi-Demensional Arrays on Heap

1. Originally posted by The Dog
Doh, my bad!

I didn't see this : new Room[20];

I assumed it was : new Room;

I'm sorry Enmeduranki, but i was following my code instead of Fordy's.

::Throws the dog a bone::

2. wow, this is a pretty heated argument... I agree with Enmeduranki. 20 arrays of 50 chars each makes 1000 chars in total, not 50. This is because of the ancient and nearly-forgotten mathematics technique calles "multiplication", which states that "20 X 50 = 1000". And an array of 20 arrays of 20 rooms each is 20 arrays of 20 rooms each, or 400 rooms in total.

In the previous code we had 20 pointers, so we created 20 Room objects on the heap.
So thats 20 pointers to arrays of 20. And each pointer is turned into an array of 20 arrays of 20 rooms each.

As for the concept of "20 pointers = 20 rooms", lets look at this: If you have 1 array of 20 rooms, then you have 1 pointer, and 20 rooms. So "1 pointer = 20 rooms". so "20 pointers = 20 X 20 rooms".

3. btw, i still stand by my code!

>>
//dynamically allocated an array of pointers
//this isn't necessary if one of your dimensions
//is a compile time constant
int** array = new int*[20];

//allocate an array of 20
//integers (yes, 20 and they're integers you know)
//to each of the previously allocated pointers

array[0]=new int[20];
array[1]=new int[20];
array[2]=new int[20];
array[3]=new int[20];
array[4]=new int[20];
array[5]=new int[20];
array[6]=new int[20];
array[7]=new int[20];
array[8]=new int[20];
array[9]=new int[20];
array[10]=new int[20];
array[11]=new int[20];
array[12]=new int[20];
array[13]=new int[20];
array[14]=new int[20];
array[15]=new int[20];
array[16]=new int[20];
array[17]=new int[20];
array[18]=new int[20];
array[19]=new int[20];

There's quite a few 20s there.
<<
Yet array only holds 20 arrays, each which hold 20 chars!

4. i think i made it work also, but with one less * (asterik).
See anything wrong people??..
Code:
```ROOM** createRooms()
{
// for a 20x5 array of ROOMS

ROOM** room;
room = new ROOM*[5];    //room now holds the address to the first of 5 pointers to ROOM objects

for (int i=0; i<5; i++)
room[i] = new ROOM[20];    //room[0] holds address of first of 20 ROOMS

return room;    //return a ROOM**
}

//..somewhere in main()

for (int i = 5; i<5; i++)
delete [] room[i];
room = 0;```

5. *forget the post above*
Ah, the traditional old "need glasses" problem...

6. Man, what a lot of repies

> ROOM** createRooms()
This is fine - and if you understand it, use it. There is just one small point to add

for (int i = 0; i<5; i++)
&nbsp;&nbsp;delete [] room[i];
delete [] room; // add this
room = 0;

Like I said, if you're happy you understand it, go with it, and ignore my more unusual pointers

The rest of this is an explanation of my previous posts. Pointers to arrays are a bit unusual, and take some getting used to.

> 1)what does the Lvalue on the assignment statment mean? the [20] slightly confuses me.
Ok, there are two very similar looking declarations which mean two very different things.

ROOM (*room)[20];
This is just ONE pointer, but it is a pointer to an array of 20 ROOMS
See my previous post on how to initialise this

ROOM *room[20];
This is an array of 20 pointers, each of which points to an array of ROOMS. This is initialised using
for(x = 0;x < 20;x++) room[x] = new ROOM[20];

Both give you the same room[20][20] structure by slightly different routes.

> 2)how can i return the room pointer back to main? what return type do i use?
Now this is where it gets a bit tricky.
Code:
```#include <iostream>
using namespace std;

class ROOM {
int    id;
};

// This is the hard way
// pointers to arrays have a really weird looking notation
// which gets even weirder when you try and return such a pointer
// from a function.
// trust me, you really dont want to do it this way
ROOM (* hard_way( void ) )[20] {
ROOM (*p)[20];
p = new ROOM[20][20];
return p;
}

// This is much better, we use a typedef to create a new
// name for the row of the array
// ROOMROW is an array of 20 ROOMS
typedef ROOM ROOMROW[20];

// See how much better this looks, even though
// it is identical to the previous example
ROOMROW *easy_way ( void ) {
ROOMROW   *p;
p = new ROOMROW[20];	// 20 lots of ROOM[20]
return p;
}

int main() {
ROOM     (*p)[20] = hard_way( );
ROOMROW   *q = easy_way( );

// we now have p[10][20] and q[10][20]
for ( int r = 0 ; r < 20 ; r++ ) {
for ( int c = 0 ; c < 20 ; c++ ) {
p[r][c] = q[r][c];
}
}

delete[] p;
delete[] q;
return 0;
}```

7. Okay all - thanks so much! I'm pretty sure that it works now. I guess it was so hard for me cause I have trouble looking at code and understanding what it does.
Well if any of you are interested and want some gratification (is that the right word?) of your good services, here's the code/program that i've been working on - you all can download it if you want just to see it.. err.. well.. umm.. whatever.

Thanks again!!
(will post the program once i restart computer)

8. here it is so far

9. ## Passing Constant Pointers to Pointers

I have a pointer to a pointer and I'd like to pass that to a function, and have that function accept is as a constant thingy, so that function can only call const member functions (does that make sense?) I can't seem to get it to work though.. here's a little sniplett..
Code:
```int main()
{
//...

ROOM** roomPtr;
roomPtr = createRooms();   // works great

functionOne(roomPtr);

//...
}

functionOne(const ROOM** roomPtr)
{
//now this roomPtr should only call const member functions of ROOM type
}```
BTW, i get get it to work easily enough for a regular pointer (like functionTwo(const MYCLASS* classPtr)...

10. I think you need to apply the const to both levels of indirection

Like so
Code:
```void functionOne(const ROOM* const* roomPtr)
{
//now this roomPtr should only call const member functions of ROOM type
}```
This prevents you from modifying a roomPtr[i] with say
roomPtr[i] = new ROOM

And prevents you from modifying a data element with say
roomPtr[i][j].fred = 1;

11. thakns so much salem.. i'll try that out

Popular pages Recent additions