0

is there any way to save the arrays that are generated bt the make_arr() function so that i can access or modify that array contents outside the void function or is there any way that i can return the array

#include <iostream>
using namespace std;

void make_arr(){
    int x =0;
    cin >> x;
    int arr[x] = {};
    for (int i = 0; i < x; i++){
        cin >> arr[i];
        cout << "-------> " << arr[i] << endl;
    }
    for  (int i= 0 ; i < x; i++) { cout << arr[i] << endl; } 

}
int main()
{
   int n;
   int q; 
   cin >> n >>  q;

   for (int i; i < n; i++) { 
   make_arr();

   }
   return 0;
}

i am using gcc 7.2

tochka
  • 29
  • 4
  • 3
    `cin >> x; int arr[x] = {};` -- This is not valid C++ syntax. Arrays in C++ must have their sizes denoted by a constant expression, not a variable. Use `std::vector arr(x);` -- if you had done that, then you would have had no issues passing or returning items. – PaulMcKenzie Feb 27 '20 at 15:53
  • 1
    Use a `std::vector` instead of the array, and return that. Job done! – Bathsheba Feb 27 '20 at 15:54
  • 2
    `for (int i; i < n; i++) { ` uses an uninitialized `i`. – mch Feb 27 '20 at 15:58
  • Use a vector. That array is not even a standard C++ array, and there is no way to return it. – Sam Varshavchik Feb 27 '20 at 16:01
  • after i edited my code to use the 'std::vector arr(x);' i still get an error that i can't return the from the void function please explain why in depth – tochka Feb 27 '20 at 16:10
  • 1
    For that error you need to refresh some very basic C++ knowledge. If you want to return something from a function, what do you need to declare the functions return-type as? – Some programmer dude Feb 27 '20 at 16:19

0 Answers0