6

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?

nwinkler
  • 52,665
  • 21
  • 154
  • 168
sajid
  • 63
  • 1
  • 1
  • 3

7 Answers7

9

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);
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • sir i have defined and call the function in this way but its giving me error that entries is not defines in this scope .array is not defined in this scope and what to write inplace of size – sajid Jun 21 '11 at 11:15
  • @sajid, in C++ the array needs to be of fixed size. Suppose, if you find it difficult to evaluate then you can simply pass by pointer also. See, I have edited my answer. – iammilind Jun 21 '11 at 11:19
4

Two lines changed:

std::vector<double> hour_payload_add(int entries , std::vector<double> array) // HERE
{
    int index=0 ,k=0;
    int totalpayload=0;
    std::vector<double> returnarray(entries/120);                            // HERE

    /// ....

    return returnarray;
}

Okay, maybe add one at the top

#include <vector>

For full bonus, I suggest changing it a bit more:

std::vector<double> hourgraph(hrentries);
hourgraph = hour_payload_add(graph);

// ...
std::vector<double> hour_payload_add(std::vector<double> array)
{
    const size_t entries = array.size();
sehe
  • 374,641
  • 47
  • 450
  • 633
1

You cannot return arrays in C++ - you would rather return a pointer to a piece of memory.

Moreover, you cannot return a pointer to a locally declared array that is on the stack because the stack is restored right after the function terminates. You have to allocate it dynamically with new or malloc, and return a pointer to its beginning.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
1

For that you should be using a pointer and create the array before you fill it (not in the function that is filling it).

double* hour_payload_add(int entries , double array[])

However I prefer to pass arrays as a reference.

void hour_payload_add(int entries , double array[], double& ret_array[])

Or even better (as others will say) would be to use vectors.

void hour_payload_add(std::vector<double>array, std::vector<double>& ret_array)

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
  • Sir should i have done it using your 3rd method i have call it in this way hour_payload_add(entries ,graph ,&hourgraph); sir kindly guid me where i am wrong – sajid Jun 21 '11 at 09:35
  • Take a look at @sehe answer - he has given you the complete function. – graham.reeds Jun 21 '11 at 09:37
1

You have to create your array using new. So your function should be like this:

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

    }

    return returnarray;
}

After using the array you must free the space by calling delete[] on the array to avoid memory leaks:

int entries = 5;
double* array = hour_payload_add(entries);
for (int i=0; i < entries; i++)
{
    std::cout << array[i] << std::endl;
}

delete[] array;
Stephan
  • 4,187
  • 1
  • 21
  • 33
  • 2
    +1 for mentioning the importance of the `delete[]`, even tho' I think that using a `vector` would be a safer solution :) – icabod Jun 21 '11 at 10:04
  • Indeed. This is messy because the calling scope must take ownership of the allocated block of memory. Also, the OP specifically asked for a pointer-less solution. – Lightness Races in Orbit Jun 21 '11 at 11:39
1

In general you should not use arrays in C++, instead preferring the use of the STL Container Classes (std::vector is the most array-like of these). Of course there are exceptions, and in some cases you should use an array (never say never), but in your example I would suggest the use of vector instead.

Sehe's answer provides an example of using the std::vector, and initialising it with the size of the array:

std::vector<double> returnarray(entries/120);

This uses a constructor that sets the default size of the array. More usefully a vector can change size dynamically, and you can, as suggested, return it from a function. Depending on the optimisation done by the compiler, it may not even create a copy of the vector - but this is compiler-dependent.

My suggestion is to look at documentation on vector and other containers in preference to an array.

icabod
  • 6,992
  • 25
  • 41
0

You cannot return an c style Array from a function in C++. But Yes you can return a std::array container from a function

You can return starting address of the c style array which is created locally on inside the function but that array gets cleared off when function returns and you are left with a pointer pointing to not what you wanted.

You will have to dynamically allocate a array using new and then return pointer to it.

Alok Save
  • 202,538
  • 53
  • 430
  • 533