Search code examples
c++arraysvectordynamic-memory-allocationsmart-pointers

Returning an array from a function to main in c++


How can I return this array to the main function for further use in my program?

int* enterMarks(int marks) {

int arr[marks];
cout << "Please enter " << marks << "marks (0<=Mark<=100):" << endl;

for (int i = 0; i < marks; i++) {
    cout << i+1 << ">";
    cin >> arr[i];
}
cout << "Thank you..." << endl;

return *arr;

Solution

  • For starters variable length arrays like this

    int* enterMarks(int marks) {
    
    int arr[marks];
    //...
    

    is not a standard C++ feature. In C++ the size of an array shall be a compile-time constant.

    Secondly a pointer to a local array with automatic storage duration returned from the function will have invalid value because the local array will not be alive after exiting the function.

    You need to have a dynamically allocated array.

    Either you can use the smart pointer std::unique_ptr that will point to a dynamically allocated array as shown in the demonstrative program below.

    #include <iostream>
    #include <memory>
    
    std::unique_ptr<unsigned int[]> enterMarks( size_t marks ) 
    {
        const unsigned HIGH_MARK = 100;
    
        auto arr = std::make_unique<unsigned int[]>( marks );
    
        std::cout << "Please enter " << marks 
                  << " marks (0<=Mark<=" << HIGH_MARK << "):" << std::endl;
    
        for ( size_t i = 0; i < marks; i++ ) 
        {
            std::cout << i+1 << "> ";
            std::cin >> arr[i];
        }
    
        std::cout << "Thank you..." << std::endl;
    
        return arr;
    }
    
    int main() 
    {
        size_t n = 10;
    
        auto arr = enterMarks( n );
    
        for ( size_t i = 0; i < n; i++ )
        {
            std::cout << arr[i] << ' ';
        }
        std::cout << '\n';
    
        return 0;
    }
    

    The program output might look like

    Please enter 10 marks (0<=Mark<=100):
    1> 10
    2> 20
    3> 30
    4> 40
    5> 50
    6> 60
    7> 70
    8> 80
    9> 90
    10> 100
    Thank you...
    10 20 30 40 50 60 70 80 90 100 
    

    Or you can use the standard class template std::vector as shown below.

    #include <iostream>
    #include <vector>
    
    std::vector<unsigned int> enterMarks( size_t marks ) 
    {
        const unsigned HIGH_MARK = 100;
    
        std::vector<unsigned int> arr( marks );
    
        std::cout << "Please enter " << marks 
                  << " marks (0<=Mark<=" << HIGH_MARK << "):" << std::endl;
    
        for ( size_t i = 0; i < marks; i++ ) 
        {
            std::cout << i+1 << "> ";
            std::cin >> arr[i];
        }
    
        std::cout << "Thank you..." << std::endl;
    
        return arr;
    }
    
    int main() 
    {
        size_t n = 10;
    
        auto arr = enterMarks( n );
    
        for ( size_t i = 0; i < n; i++ )
        {
            std::cout << arr[i] << ' ';
        }
        std::cout << '\n';
    
        return 0;
    }
    

    The program output might look the same way as shown above.

    Pay attention that there is no great sense to maje the array of the signed integer type int. It is better to use unsigned integer type unsigned int.