| 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.)
|