Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an array from a function [duplicate]

Tags:

c++

arrays

Here is my code

double hour_payload_add(int entries , double array[])
{
    int index=0 ,k=0;
    int totalpayload=0;
    double returnarray[entries/120];
    while(k< entries)
    {
           totalpayload = totalpayload + array[k];
            if(k%120==0)
                {
                returnarray[index] = totalpayload;
                index++;
                totalpayload=0;
                }

    }

return returnarray;
}

here I have called it

double hourgraph[hrentries];
 hourgraph= hour_payload_add(entries , graph);

as I want to return an array what should I do to return without using pointers?

like image 272
sajid Avatar asked Jun 21 '11 09:06

sajid


1 Answers

Pass the array by reference to the function and don't return anything. Small example:

void hour_payload_add(int entries , double array[], double (&returnArray)[SIZE])
{
  // returnArray will be updated as it's external to function.
}

or

void hour_payload_add(int entries , double array[], double *returnArray) // <-- pointer
{
  // returnArray will be updated as it's external to function.
}

usage:

double returnArray[SIZE];
hour_payload_add(entries, array, returnArray);
like image 199
iammilind Avatar answered Nov 07 '22 19:11

iammilind