C++ Pointer Creating and Using Dynamic Arrays

Description

C++ Pointer Creating and Using Dynamic Arrays

#include <iostream>
#include <new>

using namespace std;

int main()//from   ww  w  .  j a v  a2  s .  co m
{
    int HowMany;
    int* DynArray;

    cout << "How many numbers would you like?" << endl;
    cin >> HowMany;

    DynArray = new (nothrow) int[HowMany];

    if (DynArray == nullptr)
        cout << "Error: Could not allocate memory!";
    else
    {
        for(int i = 0; i < HowMany; i++)
            DynArray[i] = i;

        cout << "Displaying entries:" << endl;
        for (int i = 0; i < HowMany; i++)
            cout << DynArray[i] << endl;

        delete[] DynArray;
    }
    return 0;
}



PreviousNext

Related