Well, it would be wise to return the size as well as the pointer, because otherwise there'll be no way of using the resulting array safely.
So we're looking for a way to return two values from a function. This can be done by taking a reference parameter, through which you assign one of the values:
int *array_(int &size) {
std::cin >> size;
int *Array = new int[size];
for (int i = 0; i < size; ++i)
std::cin >> Array[i];
return Array;
}
int main() {
int size;
int *arr = array_(size);
// ...
delete[] arr; // Don't forget to delete[] what has been new[]'d!
}
Or, you could return a std::pair
containing both values:
std::pair<int *, int> array_() {
int size;
std::cin >> size;
int * Array = new int[size];
for (int i = 0; i < size; ++i)
std::cin >> Array[i];
return {Array, size};
}
int main() {
auto arr = array_(size);
// ...
delete[] arr.second; // Don't forget still!
}
But both of these are crazy bad ideas, so you could start right now to write actual C++ so it doesn't look like deformed C, using standard containers:
std::vector<int> array_() {
int size = 0;
std::cin >> size;
std::vector<int> vec(size);
for(int &i : vec)
std::cin >> i;
return vec;
}
int main() {
auto arr = array_(size);
// ...
// Now you can forget delete: no raw owning pointer, no worries.
}
Simple, leak-proof and exception-safe (well, ideally you'd sanitize user input too).