An array can not change size; you can not add elements to an array.
However, a std::vectorcan change size, and you can add elements to it. http://www.cplusplus.com/vector
Also, it is proper to teach/learn vectors before arrays (arrays are much more difficult)
The only way to change the size of an array is to make it dynamic, and have it so that when you want to increase the size, you create the second array, transfer all values, and then delete the first array. Obviously, this method requires much more code and is far less efficient than just using vectors, which are built specifically for the problem you are having to deal with here.
//take in an array of values, and return a new array with a new value
template <class Utype>
Utype extend_arr(Utype arr[], int length, Utype* val)
{
Utype array2 = new Utype[length + 1];//create the new array
for (int J = 0; J < length; J++)
array2[J] = arr[J];//fill it with the values from the previous array
array2[length] = val;//attach the new value to the end of it
delete[] arr;
arr = NULL;
return array2;//return the array
}
@Smac89 your code is incorrect.
-extend_arr's return type should be a pointer, currently it can only return an element of the array, which makes line 14 invalid.
-val should be passed by const reference, currently passing by pointer makes line 9 incorrect.
-Setting arr to NULL on line 12 does not affect arr outside the function.
Really, arr should be a pointer passed by reference and extend_arr should return void - the function should modify what the pointer points to so there is no risk of dangling pointers.
And honestly, this much code versus using a vector only means room for mistakes (such as the mistakes you demonstrated).