-1

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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Use `std::vector` instead. You can return one as you would any other object. – Igor Tandetnik May 31 '20 at 22:19
  • Turn on your compiler's warnings. It should have been able to warn you about this mistake. – Eljay May 31 '20 at 22:30
  • You shouldn't be returning references or pointers to locally (stack) declared variables. Return either a pointer to a value stored on the heap or a copy. – Varun Nayak May 31 '20 at 22:34
  • It's impossible to return an array from a function in C++. In your code you've defined your function to return `int*` which is a pointer not an array. Use a `std::vector` instead. It is possible to return a vector from a function. – john May 31 '20 at 22:40
  • Idiomatic C would be to have the caller allocate the array and pass it into the function that fills it `int enterMarks(int *arr, int marks) {...}`. In C++ you should be using a `vector` instead. – dxiv May 31 '20 at 22:45
  • 1
    Does this answer your question? [How to return an array from a function?](https://stackoverflow.com/questions/4264304/how-to-return-an-array-from-a-function) –  May 31 '20 at 22:50

3 Answers3

2
int arr[marks];

This is a local variable and so you cannot return it, otherwise the behaviour is undefined, caused crashing etc.

You have two options

1) Allocate the array in the heap and return it

int *arr = (int *)malloc(sizeof(int)*marks);
...
return arr;

You must then free the arr in the calling function.

2) Better, use C++ vector and return it

std::vector<int> arr;
...
cin >> mark;
arr.push_back(mark);
...
return arr;

This way you don't have to remember to free the heap memory.

artm
  • 17,291
  • 6
  • 38
  • 54
2

You should not return a pointer to a variable you define in the local scope of your function i.e. in your case arr.

Return a copy of std::vector instead.

#include <vector>


vector<int> enterMarks(int marks) {
  vector<int> arr;
  cout << "Please enter " << marks << "marks (0<=Mark<=100):" << endl;
  for (int i = 0; i < marks; i++) {
    cout << i+1 << ">";
    int mark;
    cin >> mark;
    arr.push_back(mark);
  }
  cout << "Thank you..." << endl;

  return arr;
}

To print these value out in main, you can simply do the following.

vector<int> arr = enterMarks(marks);
for (int i = 0; i < arr.size(); i++) {
  cout << "Mark " << i << " = " << arr[i] << endl;
}
Varun Nayak
  • 290
  • 1
  • 7
2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335