473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to return a 2-D array

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.
Oct 5 '07 #1
6 2076
Ganon11
3,652 Expert 2GB
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.)
Oct 5 '07 #2
RRick
463 Expert 256MB
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.  
Oct 5 '07 #3
Studlyami
464 Expert 256MB
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.
Oct 5 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 6 '07 #5
Thanks for the replies,

I guess the RRick solution works for me...

Thanks,
Oct 7 '07 #6
RRick
463 Expert 256MB
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.
Oct 8 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
5
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
5
by: shashi59 | last post by:
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.