I tried to create a program that adds 2 arrays of different sizes. But I would like to know to dynamically increase the capacity of an array? Example: array [4], then update the size to 2 to create an array [6] ;? EDIT: using vectors
I tried to create a new ptr, but it does not work. I get an error: a read-only variable is not assigned.
int *ptr2 = new int[a2.size];
for (int i=0; i<(a1.size); i++) {
ptr2[i] = a1.ptr[i];
}
for (int i=0; i<(a2.size); i++) {
ptr2[i] += a2.ptr[i];
}
delete [] a1.ptr;
a1.ptr=ptr2;
source
share