Archived

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

ATDguy

pointer to 2d arrae

Recommended Posts

ATDguy    122
how do i declare a pointer to a 2d array will it be the same as 1d ? is there any container that can acts like a dynamic 2d array which size i can change ? thanks

Share this post


Link to post
Share on other sites
Matt Calabrese    122
All a 2D array is, like a 1D array, is a pointer to the first element of the consecutively allocated data. No matter how many dimensions the array is, it can always be pointed to by using a pointer to the first element.
As far as "size changing" goes, you really can''t do as much with regular arrays (because you''d have to take into account the fact that if you were to make the array larger, there might be space occupied ahead of it). If you want to be able to "resize" that array, just encapsulate a pointer to the datatype in a class (templating for modularity), and have size changes dynamically allocate a new array, delete the old one, and have the pointer point to the new array.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
actually you can''t do char* parray = new char[10][10]. One way around it is to just a single array char* parray= new char[100] or declare a pointer to a pointer char** parray, allocate the number of rows u want, then loop through each row and allocate the number of columns. for ex.:

char** parray = new char*[10];
for(int i=0;i<10;i++)
parray = new char[10];

However, when you do this you won''t necessarily get a linear contiguous block of memory, so keep that in mind.

Share this post


Link to post
Share on other sites