// dynamically allocating and filling array in a function example // Mikhail Nesterenko // 3/12/12 #include using std::cout; using std::endl; using std::cin; void fillDynamicArray(int*&, int&); // function that fills the array int main() { int *dynamicArray, arraySize; fillDynamicArray(dynamicArray, arraySize); // fills the array // prints the array out for(int i=0; i < arraySize; ++i) cout << dynamicArray[i] << " "; cout << endl; delete [] dynamicArray; // deallocates the array } // fills the array // array pointer and its size are passed by reference void fillDynamicArray(int *&array, int &size){ cout << "Enter array size: "; cin >> size; array = new int[size]; cout << "Enter " << size << " integers: "; for (int i=0; i < size; ++i) cin >> array[i]; }