<to*******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi,
I got this problem when try to used the pointer
sample code:
int *A;
A= new A[10];
Do you mean...
int *A = new int[10];
? A is the array name, int is the type...right?
//...some codes here add data
A= new A[20];
Same here.
// .. data is lost
So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration. I
don't want to lost the data. And also I want to enlarge the array size.
I don't know how to solve this. Plz help me.
You can't "resize" the array. But you can create a different one (with a
different name, say, B), and the size you want. Then, copy what you need
from A to B. Then delete [] A. After that, you can either work with B, or
copy the B pointer to A and work with A.
You should note, this is not the best way to handle resizeable arrays. The
std::vector class is much better at doing this kind of thing, and more
easily and safely. Plus, if you're creating an array of objects (instead of
just ints), then the work of copying from A to B described above is very
wasteful.
-Howard