Im trying to understand pointers but im having trouble increasing the size of an array, what im trying to do is make an array that is one bigger than the standard array, copy over everything there, then delete the old pointer array, create a new pointer array that is the right size and move back everything from this temp array. For some reason I keep getting "Unable to read memory" and I have no clue why.
#include <iostream>
int main()
{
int number;
int arraySize = 3;
bool full;
int *ptr = new int[arraySize] {0};
do
{
full = false;
std::cout << "Input a number please: ";
std::cin >> number;
getchar();
for (int i = 0; i < arraySize; i++)
{
if (ptr[i] == 0)
{
ptr[i] = number;
i = arraySize;
}
else if(arraySize -1 == i)
{
full = true;
}
}
if (full == true)
{
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; x++ )
{
tempPtr[x] = ptr[x];
}
delete[] ptr;
arraySize++;
int *ptr = new int[arraySize];
for (int x = 0; x < arraySize; x++)
{
ptr[x] = tempPtr[x];
}
ptr[arraySize] = number;
}
}while(number != -1);
for (int z = 0; z < arraySize; z++)
{
std::cout << ptr[z] << std::endl;
}
getchar();
return 0;
}