allocating memory for a pointer to a two dimensional array
I have a pointer which is meant to point to a two dimensional array like this:
pointer[1][2]
I know that the second dimension always is nine, but I don''t know the first dimension:
pointer[?][9]
I need to allocate memory dynamicaly for this.
How do I do it?
When I want to do this:
int ArraySize = 10;
int *pointer;
pointer = new int[ArraySize][9];
The compiler tells me:
error C2440: ''='' : cannot convert from ''short (*)[9]'' to ''short *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Any ideas how to do this?
"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
How about:
int ArraySize = 10;
int **pointer;
pointer = new int *[ArraySize];
for (int index=0; index < ArraySize; index++)
{
pointer[index] = new int [9];
}
Note: I'm just a beginner programmer; and I'm using this sort of call in a class (i.e. "this->pointer = new int *[ArraySize]") - so I don't know if you might need some pointer-dereferences ("*") in front of "pointer" in the above code... see what the compiler says.
Good luck!
--HB
P.S. In the above example, that's for a 10 by 9 array. ArraySize becomes the variable holding your first-dimension size.
Edited by - HBringer on November 9, 2000 6:17:38 PM
int ArraySize = 10;
int **pointer;
pointer = new int *[ArraySize];
for (int index=0; index < ArraySize; index++)
{
pointer[index] = new int [9];
}
Note: I'm just a beginner programmer; and I'm using this sort of call in a class (i.e. "this->pointer = new int *[ArraySize]") - so I don't know if you might need some pointer-dereferences ("*") in front of "pointer" in the above code... see what the compiler says.
Good luck!
--HB
P.S. In the above example, that's for a 10 by 9 array. ArraySize becomes the variable holding your first-dimension size.
Edited by - HBringer on November 9, 2000 6:17:38 PM
yeah that's how you do it
just though I'd add the delete
i's don't show up in [] because it think you mean italics... guess I'll just use j's for now on
Edited by - Magmai Kai Holmlor on November 9, 2000 6:37:31 PM
just though I'd add the delete
int rows=6, cols=9;int** Array2D;Array2D = new int*[rows];for(int j=0;j<cols;j++) Array2D[j] = new int[cols];//...for(int j=0;j<cols;j++) delete[] Array2D[j];delete[] Array2D;
i's don't show up in [] because it think you mean italics... guess I'll just use j's for now on
Edited by - Magmai Kai Holmlor on November 9, 2000 6:37:31 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Ooohhh, good catch! I almost forgot about those; but since all my stuff is OOP, I have those tucked away in Destructors and cleanup functions (outta sight, outta mind)... :-P
--HB
--HB
Uhhh, thanx guys.
Works perfectly. I was pretty busy in the last few days and so I couldn''t answer earlier.
Anyway: you were a great help once again.
Thanks!
"Mr Sandman bring me a dream"
Works perfectly. I was pretty busy in the last few days and so I couldn''t answer earlier.
Anyway: you were a great help once again.
Thanks!
"Mr Sandman bring me a dream"
-----------------------------"Mr Sandman bring me a dream"
wouldn''t it work to just do this:
Of course, you would have to do something like this to access the array 2-dimensionally: int iSomeInt = lpIntArray[X + Y * 9];
int iArraySize;int * lpIntArray;lpIntArray = new int[9 * iArraySize];
Of course, you would have to do something like this to access the array 2-dimensionally: int iSomeInt = lpIntArray[X + Y * 9];
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement