Jump to content
  • Advertisement


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


pointer to 2d arrae

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
char * pArray = new char [10][10];

should be

char (*pArray)[10] = new char [10][10];

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!