| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
#1
|
|||
|
|||
|
Why does this work:
char * map = new map[x] but this doesnt: char map = new map[x][y] ??? Is there a way to create multi-dimensional arrays on the heap?? |
|
#2
|
|||
|
|||
|
Re: Multi-dimensional arrays on the heap
4th line should be:
char * map = new map[x][y] |
|
#3
|
|||
|
|||
|
Re: Multi-dimensional arrays on the heap
Exact error:
cannot convert `char (*)[6]' to `char*' in initialization |
|
#4
|
|||
|
|||
|
Re: Multi-dimensional arrays on the heap
Code:
char** map = new char*[x]; // x rows for(int i=0; i < x; i++) map[i] = new char[y]; // each row points to a different 1d array. |
|
#5
|
||||
|
||||
|
Re: Multi-dimensional arrays on the heap
Instead of using arrays, you should be using vector's. They shield you from the maintenance nightmares of manual memory allocation and provide more functionality than dynamic arrays.
You can use a multi-dimensional vector like this: Code:
#include <vector>
int main()
{
// create a matrix of 4 x 3 filled with 0
std::vector<std::vector<int> > matrix(4, std::vector<int>(3, 0));
matrix[0][0] = 1;
}
__________________
Cheers, D Drmmr Please put [code][/code] tags around your code to preserve indentation and make it more readable. As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky |
|
#6
|
|||
|
|||
|
Re: Multi-dimensional arrays on the heap
Why not do a search on CodeGuru? This question is one of the most asked and answered questions here. There are at least 100 threads on this topic, both with examples in C and C++, plus if you search the C++ FAQs here, you see an example.
Regards, Paul McKenzie |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|