Creating an Array With New
Arrays are dynamically allocated with brackets, and must be deleted with them as well.
#include <iostream> using namespace std; // Print the contents of the array backwards. void prback(int arr[], int size) { cout << "----------------------------------------------------" << endl; for(int i = size-1; i >= 0; --i) cout << arr[i] << " "; cout << "\n----------------------------------------------------" << endl; } int main() { // Allocate an array dynamically with new. This is the only // standard way to allow the size to be a variable, though gcc will // variable-sized regular arrays as an extension. Ask for the // size, then allocate. int size; cout << "How big would you like that array, sir? "; cin >> size; int *arr = new int[size]; // Read it in. for(int i = 0; i < size; ++i) cin >> arr[i]; // Print it out backwards (using a function). prback(arr, size); // This is the correct way to delete an array. The brackets // remind the system that it was an array. It forgot. delete [] arr; }