1

I am trying to return a certain array from a function that when called, assigns value to the array

The function should go something like this:

int arr[10];
int values[10] = {1,2,3,4,5,6,7,8,9};
for (int i =0; i<10; i++)
{
arr[i] = values[i];
}

How do I translate this code into a function?

using namespace std;

2
  • 3
    Consider returning std::vector instead of arrays. Commented Sep 26, 2020 at 0:24
  • 5
    Use std::array, not dumb arrays, which are not copyable. Commented Sep 26, 2020 at 0:30

3 Answers 3

2

As far I know, you shouldn't be looking to return an array from a function... like, ever. What I would do instead is pass the names of the arrays to the function and have it process the contents in whatever way you need it to. In your case, you're just copying them so something like this should work:

#include <iostream>
using namespace std;

void copy_array (const int [], int []); //function prototype

int main()
{
    //Your arrays
    int arr[10];
    int values[10] = {1,2,3,4,5,6,7,8,9};

    //Function call
    copy_array(values, arr);

    //Display contents of array
    for(int i =0; i<10; i++){
        cout << arr[i] << " " ;
    } 

    return 0;
}

//Function definition
void copy_array (const int values[], int arr[]){
    
    for (int i =0; i<10; i++){

        arr[i] = values[i];

    } 

}

Output

1 2 3 4 5 6 7 8 9 0 

Also, your array size should be a constant integer variable.

Sign up to request clarification or add additional context in comments.

2 Comments

How is this different than the other answer that was given? Anyway, the comments I made to that answer applies to your answer also.
I didn't notice the other answer... I guess I took too long responding lmao... mb
1

you probably don't want to return arrays like never, because it could mean a lot of useless copy, which slows down your program, but you could always write a function that copies values from one array to another

#include <iostream>

void assignarray(int *dest, int *src, size_t size)
{
    for (size_t i = 0; i < size; ++i)
    {
        dest[i] = src[i];
    }
}

int main()
{

    int arr[10];
    int values[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    assignarray(arr, values, sizeof(values) / sizeof(int));
    for (size_t i = 0; i < 10; i++)
    {
        std::cout << values[i] << " ";
    }
}

output:

1 2 3 4 5 6 7 8 9 10

6 Comments

Your comment about it being "useless copies" is not necessarily true with an optimizing compiler. But arrays cannot be returned anyway, so that's a moot point. I would bet that your code may not be any faster, and possibly slower than using std::array.
The output I got is 1 2 3 4 5 6 7 8 9 10 . The 4th number differs from your output.
yeah sorry I copied a wrong output from an earlier version :D @PaulMcKenzie if you can only use std::array if you have fix sized arrays and you know them compile time. if you use a copy method you can resize it and if you know the size you can always copy them :D but yes if you can use std::array you should do that
To resize an array, you would use std::vector. One thing you should realize -- introducing pointers does not mean a "code speedup". Too many still think that using pointers will outperform a compiler's optimizer. Also, the comment of returning an array -- even though arrays cannot be returned, that basic comment is not true about returning will slow down the code. Compilers use, and in some cases, mandated to have return-value-optimization, thus all of those fears from 20 years ago about returning large objects "slowing down" a program are for the most part, gone.
And if you have a small program with little functionality why would you wrap it inside a heavy std::vector class ? -- That is why I mentioned std::array the first time. Second, the amount of functionality has nothing to do with using a vector or not. If the functionality of what vector provides creates a clean, non-buggy program, then it should be used.
|
0

Return type of a function cannot be an array.

However, array can be member of a class, and class can be return type of a function, so it is possible to return a class object which wraps the array. The standard library has a template for such array wrapper. It is called std::array.

That said, returning an array (wrapped within a class) is not necessarily a good design. Sometimes it is better to let the caller create the container - and indeed, they can choose the type of the container that they wish to use - and pass that into a template function to be filled. Here is an example of accepting a range:

template<class Range>
void fill(Range& r) {
    assert(std::ranges::distance(r) == 10);
    int values[10] = {1,2,3,4,5,6,7,8,9};
    int i = 0;
    for (auto& e : r)
    {
         e = values[i++];
    }
}

// example
int arr[10];
fill(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.