Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

how to return a multidimensional array?

This topic is 4844 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

I have been checking some websites but I cannot figure out how to return a multidimensional array.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by doja93
return a pointer or double pointer


Multidimensional array != array of arrays.

Share this post


Link to post
Share on other sites
Just pass in references to the data you need returned:


void returnArrayFunction( int &array_x, int &array_y, int **&arrayData )
{
//for example...
array_x = 10;
array_y = 10;

arrayData = new int[array_x];
for (int cx=0;cx<array_x;cx++)
arrayData[cx] = new int[array_y];
}


//in calling function...

int arraySizeX,arraySizeY;
int **arrayData;

returnArrayFunction( arraySizeX, arraySizeY, arrayData );





I assume this is what you mean

[edit] this is assuming you don't neccasarily know the dimensions of the new array [/edit]

[S]

Share this post


Link to post
Share on other sites
If you're using C++, consider looking into vectors. They are very similiar to arrays and can be returned.
Take a look at Thinking in C++ by Bruce Eckel which can be downloaded for free (though I encourage you to buy a copy). Volume 1, chapter 2 has a brief intro to vectors which is enough to get started using them.



[Edited by - Will F on May 11, 2005 2:53:59 PM]

Share this post


Link to post
Share on other sites
If you want to return any type of array, you'll somehow need to either 1) [implicitly] make an entire copy of the array before returning it, or 2) have an input to the function that says where to store the array. This could likely be in the form of a reference to a pointer which can be newed or deleted. However, I'd recommend the std::vector or boost::multi_array approach. As for how to return it, I would recommend just returning a copy of the array just like you return any other value, for functions that aren't time critical, as such a function is generally easier to use code-wise. For functions that need to be faster (or for rather large arrays), I'd recommend passing in a reference to a std::vector or boost::multi_array object.


std::vector<int> ReturnByCopy()
{
std::vector<int> FilledWithZeros(32, 0);
return FilledWithZeros;
}

void ReturnThroughParameter(std::vector<int>& FillMe)
{
FillMe.insert(FillMe.end(), 32, 0);
}

std::vector<int>& MaybeMoreConvenient(std::vector<int>& FillMe)
{
FillMe.insert(FillMe.end(), 32, 0);
return FillMe;
}

int main()
{
std::vector<int> DummyVector;

DummyVector = ReturnByCopy();
std::cout << DummyVector.size() << std::endl;

ReturnThroughParameter(DummyVector);
std::cout << DummyVector.size() << std::endl;

std::cout << MaybeMoreConvenient(DummyVector).size() << std::endl;

return 0;
}


Those examples are merely 1 dimensional, but the concepts would apply to any OO-style multi-dimensional array as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Multidimensional array != array of arrays.


Mathematically speaking, a multidimensional array will always be an array of arrays. Theoretically speaking I could make the arguement that any array is an array of arrays. Implementation details and symantics lend some truth to your statement but it certainly isn't a fact

Share this post


Link to post
Share on other sites
Alright I have been reading some more about arrays, the boost library, and vectors. So far it looks like using a 2 dimensional vector seems easiest, but I am still not sure which is the best thing to do for my situation.
Here is my situation:
I am creating a small 2d array about 5 by 5 squares. It will function as a battle field for my character and monsters to fight on. The game is a console game so the enemies and players will be represented with letters. I created the battle field 2d array in my battle class and I want to put the movement code in the parent class for all of my players and monsters. So it needs access to the battle field which is in the battle class. I don't want to make it public to ruin encapsulation. So I was just making a getField function.

Share this post


Link to post
Share on other sites
Quote:
Original post by doja93
Quote:
Original post by Fruny
Multidimensional array != array of arrays.


Mathematically speaking, a multidimensional array will always be an array of arrays. Theoretically speaking I could make the arguement that any array is an array of arrays. Implementation details and symantics lend some truth to your statement but it certainly isn't a fact


In computer science, one often cannot afford to be "mathematically speaking". ;)

Arrays of arrays superset "multidimensional arrays" as they are understood for our purposes. A multidimensional array has the additional constraint of being rectangular, which allows for simplifications in the implementation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!