This is my code
*int my_arr(int a) {
int arr[a];
for (int i = 0; i < a; i++) {
cin >> arr[i];
}
return arr;
}
I keep getting errors
You can't return an array in cpp. Instead use vectors.
vector<int> my_arr(int a) {
vector<int> arr;
for (int i = 0; i < a; i++) {
int input;
cin >> input;
arr.push_back(input); // to push elements into vector
}
return arr;
}
arr is a somewhat misleading name for a std::vector.a size for your vector when you create it, you can read directly into it's elements. std::vector<int> vec(a); for (auto &i : vec) { std::cin >> i; }
std::vectorinstead. Both of those things are legal for vectors.std::arrayorstd::vector.*inttoint*your code would (perhaps) compile, but it would still be bugged, and you are still not returning an array, you would be returning a pointer. And that pointer would be pointing at an array which no longer existed.