Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

C++, How to make a function accept a 2 dimensional array of any size?

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

Hi with c++, I want to have a function that accepts a two dimensional array of any size. eg float a[3][2] ={{1,2},{5,3},{1,9}}; float b[2][2] ={{5,3},{1,9}}; myFunction(a); myFunction(b); So i tried: void myFunction(float **var);, which doesn't work, i've got a feeling that it probably isn't possible to do this that simply. Is there any simple solution to this? Thanks alot.

Share this post


Link to post
Share on other sites
Advertisement
I was having this problem a while ago but it was with char[][]. If i remember correctly, the fcn call should look like this:

myFunction( float *var[] );

~guyaton

Share this post


Link to post
Share on other sites
Here's an example solution that Stroustrup gives.

void print_mij ( int * m, int dim1, int dim2 )
{
for ( int i = 0 ; i < dim1 ; i++ )
{
for ( int j = 0 ; j < dim2 ; j++ )
{
cout << m [ i * dim2 + j ] << '\t' ;
}

cout << '\n' ;
}
}

Share this post


Link to post
Share on other sites
That one is just a workaround by transforming a two dimensional array into one with only dimension.

Otherwise it will work and is a common practice.

Share this post


Link to post
Share on other sites
Note that microdot's solution is in reality a one-dimensional array masquerading as a 2-dimensional array -- it's been pre-packed with the values from the 2d array [EDIT: Yeah, beaten to it :D]. Still, it is a viable solution and may be easier (depending on how you're using it) than...

void myFunction(float **myarray, int dim1, int dim2) { ... }
float **a = new float*[3];
for (int i = 0; i < 3; i++)
a = new float[2];
float **b = new float*[2];
for (i = 0; i < 2; i++)
b = new float[2];
myFunction(a, 3, 2);
myFunction(b, 3, 2);

Once they've been completely initialized, you can access a and b as 2d arrays.

Share this post


Link to post
Share on other sites
Not sure if this would be relevant for your project but why not encompass the array into a class and pass the address of that class into the function. You can manipulate the array of course with access members, just as if you were manipulating the array itself.

Share this post


Link to post
Share on other sites
If you are using statically typed arrays, one option is to use templates that accept multidimensional arrays by reference. ex:

template <typename T, int r, int c>
void my_function(T (&arr)[r][c]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
std::cout << arr[j] << "\t";
}
std::cout << std::endl;
}
}

Share this post


Link to post
Share on other sites
i see thanks,

just one other question, you know when you have a class or something returning a pointer, you should use a const on the return,

should i also use a const on set functions which use pointers?
eg void setfunc(const float *val);

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!