简体   繁体   中英

How can I return an array of integers in C++

Im relatively new to C++ and was given an assignment today where we have to create a function that will take an empty array of size 25 and fill it with numbers 1 to 25. This language was never even taught in class and his example is full of errors and wont run. Here`s what I have so far:

#include <iostream>

using namespace std;

int add25(int arr[], int size);

int main()
{

    const int x = 25;
    int myArray[x];
    add25(myArray, 25);

    int i;
    for (i = 0; i < 25; i++) {
        cout << myArray[i] << endl;
    }
}

int add25(int arr[], int size) {

    int i;
    for (i = 0; i < size; i++) {
        arr[i] = i + 1;
    }
}

Obviously this only returns the first number of the array (1) and leaves the rest of it empty. How can I get it to return a number for each part of the array?

You don't have to return anything from add25() since it already operates on the array you give it a pointer to. Please not that int foo[] is the same as int *foo in a parameter list.

his example is full of errors and wont run.

The sole error in this program is that the return type of add25() should be void instead of int . Change that in both declaration and definition and the code will compile and run.

Obviously this only returns the first number of the array (1) and leaves the rest of it empty.

That's not what the program does.

The program iterates over the array indices [0...size) and assigns all of those array elements.

Then the function ends without returning any value. But the function was declared to return int . The behaviour of not returning a value from a function that was declared to return a non-void value is undefined. If you're lucky a compiler will notice it as well:

 warning: control reaches end of non-void function 

How can I return an array of integers

It is not possible to return an array from a function in C++.

However, it is possible to return class instances, and classes can contain arrays. The size of an array contained within a class must be known at compile time however. The standard library provides a class template specifically for the purpose of wrapping an array. It is called std::array . You could return std::array like this:

std::array<int, 25> add25() {
    std::array<int, 25> arr{};
    // process arr in a manner of your choosing
    return arr; // return a copy of arr
}

To avoid the limitation of the array size being constant, you can use std::vector which allocates the array dynamically.


An alternative to returning an array is to simply let the caller of the function to create the array, and only modify the array in the function.

In fact, this is what the example program does, if only the bug mentioned earlier in my answer is fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM