Returning Entire Arrays

This is a discussion on Returning Entire Arrays within the C++ Programming forums, part of the General Programming Boards category; I was wondering if I was doing this correctly. Does this line of code: Code: main() { siPlayerGrid[8][16][5] = setter(siPlayerGrid, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    51

    Returning Entire Arrays

    I was wondering if I was doing this correctly. Does this line of code:

    Code:
    main()
    {
     siPlayerGrid[8][16][5] = setter(siPlayerGrid, siCompGrid, i, j);
    }
    
    setter(short siPlayerGrid, short siCompGrid, short i, short j)
    {
     return siPlayerGrid[8][16][5];
    }
    Correctly return the whole array?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Sorry, I meant this code:

    Code:
    main()
    {
     siPlayerGrid[8][16][5] = setter(siPlayerGrid, siCompGrid, i, j);
    }
    
    setter(short siPlayerGrid[8][16][5], short siCompGrid[8][16][5], short i, short j)
    {
     return siPlayerGrid[8][16][5];
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    It´s difficult to say. I can´t see anything wrong in your code, but nethier can I see anything usefull...

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    I'm just trying to ensure that it is returning the entire array, rather than just the values at those elements, (though the subsets are [0-7][[0-15][0-4]).

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    When called, it passes the values, when returning it passes the values!

    It´s like this:

    Code:
    function(type passedbyvalue,type& passedbyreference);
    The passedbyreference variable shouldn´t be returned though!

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Using & would pass it as a pointer, how would I pass the arrays as a reference so I don't have to return them?

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    No, my friend, using & passes it as reference.
    Using * passes it as pointer.

    Try out this piece of code yourself:

    Code:
    #include <iostream>
    using namespace std;
    
    void add1(int& passedbyreference){
    	passedbyreference++;
    }
    int main(int argc, char** argv){
    	int i = 0;
    	add1(i);
    	cout << i << endl;
    }

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >how would I pass the arrays as a reference so I don't have to return them?


    actually, arrays are always passed by reference, doesnt matter if you use & or not.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Really!? Great, this makes my program more efficient since it doesn't need so many memory allocations when I send arrays to functions!

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. Multidimensional arrays returning.
    By eXeCuTeR in forum C Programming
    Replies: 33
    Last Post: 07-01-2008, 12:27 PM
  3. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  4. returning arrays from functions
    By Leeman_s in forum C++ Programming
    Replies: 11
    Last Post: 06-05-2002, 10:00 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21