How to return an array from a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chazzy69
    New Member
    • Sep 2007
    • 196

    How to return an array from a function

    Hey i have a function that assigns multiple values to an array

    e.g.

    Code:
    array[0] = 12;
    array[1] = 67;
    //etc
    But how do i return the array from the function to the main program??

    e.g.

    Code:
    main() {
    array = getarray();
    }
    
    getarray(){
    array[0] = 12;
    array[1] = 67;
    
    return array;
    }
    Any help is apprecaited, Thanks
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You'll have to pass the array (pointer) in as a parameter, or make sure that you call new/malloc on the array you plan to return. If you use the latter method, make sure you use delete[]/free on the returned array to avoid memory leaks.

    Comment

    • chazzy69
      New Member
      • Sep 2007
      • 196

      #3
      Could you possible give me an example for first method of passing a array pointer.

      Or just a link,

      Also do you mean an array of pointers??

      Thanks heaps

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        I could, but this will do a far better job than I ever could.

        Comment

        • chazzy69
          New Member
          • Sep 2007
          • 196

          #5
          I think i kind of understand what this code is doing

          http://bytes.com/forum/thread772412.ht ml

          Code:
          int* func(int arg)
          {
              int* temp = new int[arg];
              return temp;
          }
          int main()
          {
              int* arr = func(5);
          }
          So does this code work in a c compiler or only c++ ???

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            So does this code work in a c compiler or only c++ ???
            C++ only. new does not exist in C. You must use the malloc/free combination when working with dynamically allocated memory. But the concept is essentially the same. If you want to create an array in a function, and then return that array, it must be created dynamically, and you then return that pointer.

            Note that I wouldn't consider it smart to throw around raw pointers in C++ though. It's an option, but at the very least, you can wrap it in good memory management.

            Comment

            • chazzy69
              New Member
              • Sep 2007
              • 196

              #7
              Thanks for that, it might just be easier to go with globals in the end lol

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by chazzy69
                Thanks for that, it might just be easier to go with globals in the end lol
                Maybe you should read this: http://bytes.com/forum/thread737451.html.

                As regards your first question, you can return only a type or a pointer from a function. You might also read this. Your question is answered towards the end:
                http://bytes.com/forum/thread772412.html.

                Comment

                • questionit
                  Contributor
                  • Feb 2007
                  • 553

                  #9
                  Hi

                  There are atleast 3 ways for doing this:

                  1- Create an object with its member - An array. Simply return the object itself and the receiving end will also be of type object that you have created. This way your array can be returned.

                  2- Call the function that you want to return the array. When you call it, pass an array by reference not by value. Now, you dont need to return anything from the function. What ever you assign to the array variable will be available at the other part ( the function or main where you called this function from)

                  3- Make your function return type an int * and return your array by just using its name of by typing: arrayName[0] (its the same thing). The receiver of returned array will be a pointer. By doing this, the address of your array will be returned instead of values. You can advance the pointer to get all the values.

                  Hope it helps

                  regards
                  Qi

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    1 and 3 are the same thing, since an array is a pointer (they also require the use of malloc/new), and 2 only works if he's using C++. The OP never specified if he was using C or C++, so...

                    Comment

                    Working...