7

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?

3
  • 3
    This is not a C++ code as, double returnarray[entries/120]; is decided at runtime (valid in C99) Commented Jun 21, 2011 at 9:36
  • 2
    @iammilind: "Code" in this context is a non-countable noun; thus, "a C++ code" is incorrect. Just saying. Commented Jun 21, 2011 at 11:41
  • Related: stackoverflow.com/questions/4174901/… Commented Jun 21, 2011 at 11:41

7 Answers 7

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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, 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.
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();

Comments

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)

2 Comments

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
Take a look at @sehe answer - he has given you the complete function.
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;

2 Comments

+1 for mentioning the importance of the delete[], even tho' I think that using a vector would be a safer solution :)
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.
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.

1 Comment

Sure you can. std::array. Though a vector is more appropriate here, given the container's dynamic size.
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.

Comments

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.

1 Comment

std::array<int, 3> foo() { std::array<int, 3> ar{0,1,2}; return ar; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.