Home > Enterprise >  How can I resize a dynamic array of pointers without using a vector
How can I resize a dynamic array of pointers without using a vector

Time:10-27

If I wanted to resize this array:

int array[5];

I would do this:

int* temp = new int [n];
...
array = temp;

But, how would I do it if I have this array?

int *array[5];

Maybe like this?

int** temp = new int* [n];

CodePudding user response:

You CAN'T resize a fixed array, period. Its size is constant, determined at compile-time.

You CAN'T assign a new[]'ed pointer to a fixed array, either.

So, neither of your examples will work.

In your 1st example, you would need this instead:

int* array = new int [5];

And in your 2nd example, you would need this instead:

int** array = new int* [5];

Either way, don't forget to delete[] the array before assigning something new to it, eg:

int* array = new int [5];
...
int* temp = new int [n];
...
delete[] array;
array = temp;
  • Related