Thread: Return array?

  1. #1
    Registered User
    Join Date
    Sep 2004

    Return array?

    I'm writing a program in which it would be nice to return an array from a function.
    Things are working ok without such a function but for readability and "easy to reuse"
    code this would be a nice thing.
    int function(int array[20][50])
            //read coordinates from a file....
            return array;
    int main()
           //some stuff here
           int coord[20][50];
           coord = function(coord);
           //more stuff here
           return 0;
    Is it possible to do something like that by returning an array? or by reference maybe?
    Any clues would be greatly appreciated...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Even if you could return an array, you can't assign one array to another. However, there isn't a great need to return arrays either because arrays are passed as pointers when passed as parameters. That means that any changes to the array in the called function will be maintained in the calling function as well. If that isn't the behaviour you want, and you want to keep the array elements unchanged in the functions, then you can use the const keyword to prevent changes from occuring.
    You're only born perfect.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    To be a little more precise, when you pass an array as in either of the following two situations:
    void foo(int* arr);
    void foo(int arr[]);
    You are doing the exact same thing, passing a pointer to the first element of the array (in this case, arr[0]).

    So, the pointer gets copied (as always with pointer assignment). Any modifications to 'arr' will not affect whatever you passed to it (which is normal for pass-by-value semantics). However, any modifications made to *arr (or arr[0]) persist, as you are actually modifying the value pointed to by the original pointer you passed in as a parameter. So, the same deal for all of the other elements of the array.

    You can return an array as well, but only a dynamically allocated one. If you have not yet learned about dynamically allocated memory, I suggest not messing with this.

    A good alternative to your problem would be to use std::vectors.
    std::vector<std::vector<int> >
    in this case.

    I'd still recommend passing that by reference, though, as it is a rather large construct.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Sep 2004
    ok, thank you both. That is the behavior I was looking for, seems I just didn't understand what
    happened when passing arrays as function parameters. But I do now, thanks again!
    Last edited by antex; 06-02-2005 at 02:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM