Connecting Tech Pros Worldwide Help | Site Map

how to return a 2-D array

Member
 
Join Date: Apr 2007
Location: Paris
Posts: 42
#1: Oct 5 '07
Guys,

I have a private 2-D array and I am wondering how I can have a getArray() function to return it.

private:
string myarr[10][20];

I need the snippet of the code of the getArray() function! I checked all of the ways I know but none works...

Thank you in advance,

Amir.
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Oct 5 '07

re: how to return a 2-D array


What you have is a pointer to a pointer to an array of strings. There are two ways of returning this array - one is generally safer than the other, but which ever one you use depends on how you want to use the array after your getArray() function.

You can return the address that the pointer holds. In the function you call getArray() from (presumably main()?), you will now have a pointer to a pointer to an array of strings - just like you had in the class.

Benefit: it's not very messy. Your getArray() function is now really simple, and it doesn't require complicated thought on your end.

Disadvantage: If you change the array in main(), you've also changed it in your object. If this isn't what you want, don't use this method!

So, if you want any changes in main() to this array to NOT be reflected in the object, you need two separate copies of this array. You'll have to manually make a copy, element by element, into a new 2D array, and return this.

Benefit: Do whatever you want to the array returned by getArray() - it won't change anything in your object.

Disadvantage: Your getArray() function is now a little more difficult. You'll have worry about the possibility that the copies you make of the strings will be terminated after getArray() returns. You'll have the extra work of manually copying, which is going to take some more coding. It will also double the memory im[print of your program (though this probably isn't a problem for 10*20 = 200 strings.)
RRick's Avatar
Expert
 
Join Date: Feb 2007
Posts: 430
#3: Oct 5 '07

re: how to return a 2-D array


I tried making an example of where you return a pointer to the multi-dimensional array. It works, but it sure ain't pretty. It needed a typedef to get things to work. Try this link for more details http://forums.devarticles.com/c-c-he...ray-11075.html

I was not able to get the multi-dimensional array to be passed back as a value. Does anyone else have any ideas?

Unless you really need a multi-dimensional array, I would suggest using the int ** (array of pointers to array of integers) instead. The ** syntax is a lot easier to understand and code.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Array
  6. {
  7.  
  8.    int val_[3][4];
  9.  
  10. public:   
  11.    typedef int (*IntArray)[3][4];
  12.    IntArray val() { return &val_; }
  13.  
  14. };
  15.  
  16. int main( )
  17. {
  18.    Array v;
  19.    (*v.val())[1][1] = 66;
  20.  
  21.    Array::IntArray v34 = v.val();
  22.    (*v34)[0][0] = 99;
  23.  
  24.    cout << "Val: " << (*v.val())[0][0] << " VPtr: " << (*v34)[1][1] << endl;
  25.  
  26.    return 0;
  27. }
  28.  
Expert
 
Join Date: Sep 2007
Location: VA
Posts: 416
#4: Oct 5 '07

re: how to return a 2-D array


You could pass in a 2-D array into your get array and copy the private array to 2-D array you passed in. That way rather than making a copy of the array in the class function which could delete the array at the end of the function you would have the copied array located where you need to use it.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#5: Oct 6 '07

re: how to return a 2-D array


You cannot return an array from a function.

You can return a type or a pointer to a type and that's all.

Use an output argument with your array:
Expand|Select|Wrap|Line Numbers
  1. void fx(int (*IntArray)[3][4], int numelements)
  2. {
  3.  
  4. }
  5.  
Here fx has a pointer to a 3x4 array of int as an argument along with the number of elements the array. The function changes the array using the pointer.
Member
 
Join Date: Apr 2007
Location: Paris
Posts: 42
#6: Oct 7 '07

re: how to return a 2-D array


Thanks for the replies,

I guess the RRick solution works for me...

Thanks,
RRick's Avatar
Expert
 
Join Date: Feb 2007
Posts: 430
#7: Oct 8 '07

re: how to return a 2-D array


Do what you need for your application, but realize that this solution is applicable only to that specific multi dimensional (md) array. If you need to access two different md arrays, you need typedefs and functions for each one. Plus you get to work with that syntax (!!).

That's why I like the ** approach. It works for any ** array. There are downsides, mainly in creation and knowing how big the array is. In this case, you get to weigh the differences.
Reply