Reinitialize Int Array

Hi all,

I am just wondering how do i reinitialized or make a new int[] after it has been declared before. The int array i am trying to reinitialize is a global variable. Thanks
You can do this if you are working with pointers, not with arrays:
1
2
3
4
5
6
int array[5];//will always be an int[5]
int *pointer = new int[5];//dynamic allocated array
delete[] pointer;//free the memory used by pointer
pointer = new int[7]; //re-initialize to a int[7]

delete[] pointer;
Last edited on
Hi Bazzy,

Then if i would like to access the elements in the pointer array, how do i access say element 3? Do i do pointer[0]=1 as per any typical array?
pointer[3] works.
As does the uglier *(pointer + 3 )

Why not use an vector<int> ?
It's much easier to increase the size of an vector.
Topic archived. No new replies allowed.