Code:
#include <iostream>
using namespace std;
int main(void)
{
int numelements;
int *pArray=0;
cout<<"Enter the number of array elements:";
cin>>numelements;
pArray=new int[numelements];
//of course when you're done with them
if(pArray)
{
if(numelements>1)
{
delete [] pArray;
}
else if(numelements==1)
{
delete pArray;
}
pArray=0;
}
return(0);
}
To change the size on the fly, you'd want to have another pointer which you'd dynamically allocate to be the size of your original array. Then copy the contents of your original array into the temp secondary array. Next clean up your original array (delete + set it to NULL). Reallocate your original array to the new size. Copy the first group of array elements being stored in the temp array into the first part of the newly reallocated array. Finally clean up the temp secondary array.
Of course, std::vector's are allowed to grow dynamically, so you may want to look into them.