Generally speaking, how would you pass a dynamic array in C++?

Hello guys. I'm new to both C++ and the forum. I was just wondering how to pass dynamic arrays. For example, if a user inputs or creates the size of a dynamic array how would you pass both the array and the size and then return them to main? Thanks for any help!
Let's say you have:

int passArray(??????); // What would you input here?

int main();
{
int size;
int *arr;

cout << "Enter the size of the array." << endl;
cin >> size;

arr = new int[3]; //Where would you use "delete arr;", in main or the passArray function?

for (int i = 0; i < n; i++)
{
cout << "Enter the values for the array. " << endl;
cin >> arr[i];
}

}

int passArray(???????)
{
?
?
?
?

return arr; // How would you return the array to main?
}

Just like you would pass a normal array - https://www.youtube.com/watch?v=VnZbghMhfOY&index=35&list=PLAE85DE8440AA6B83&ab_channel=thenewboston

As to how to return the array - http://lmgtfy.com/?q=how+to+return+an+array+c%2B%2B

And as to the delete thing. Since you are gonna return the array, that means you need it in main right? Why else would you return it. So delete it in main.
1
2
3
4
template <typename T> T* passArray(T* array) {
    ...
    return array;
}


Since arrays are just chunks of memory, there is no way to copy one chunk of memory from place to place. So instead you pass a pointer or reference to that chunk as parameter and the same goes for returning an array, you return a pointer to the chunk of memory
Topic archived. No new replies allowed.