Input and Output array using function

I want to create a function that will accept input from the user and return the input, to be used for further calculation. I know how to accept and return with integers as parameters , but how do i do it with arrays?
EDIT: Let me make it clear.
Suppose there is an array of numbers arr[]. Now, i want a function that accepts the input from the user, and return the array for further manipulation.
For example, if the array is arr[5], then i should call a function and accept the values from the user. Then, i should return the imputed values and print the same. How can i do this.
Last edited on
1
2
3
4
5
void getInput(int arr[], size_t size)
{
    for(int i = 0; i < size; ++i)
        std::cin >> arr[i];
}

You should pass both array and its size.
http://www.cplusplus.com/doc/tutorial/arrays/
An array argument is actually passed by reference, so the function does not have a copy of array.

If you do not know the size in advance, then you have to allocate memory for the array dynamically. In that case you would pass a pointer (that points to the memory location) between functions. (And size too).
An array argument is actually passed by reference
Pointer to the first element of array is passed.
Arrays passed to functions are reduced to pointer.
After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.
C++ standard 8.3.5.5
Last edited on
Topic archived. No new replies allowed.