I am writing a function that takes in a string through pass by reference and an array of characters and the size of the array. The string already has characters in it. I am trying to erase the characters in the string and copy the characters from the array. I tried setting up a for loop to copy, but it doesn't work if the string is too small or too big. I can't use strcopy in this function. Need some guidance.
void functionname(string &first, char arr[], int size) {
int i;
(for i = 0; i < size; i++) {
first[i] = arr[i];
}
}
firsthas.size()less than thesizeargument. Sayfirstis "abc",arr[]is VWXYZ and size is 5... you copyVWXoverabc- which is allowed - but then try to copyZover thefirst[4]which is (as of C++11) guaranteed to hold a NUL but isn't legal to write to (for C++03 it may not be a NUL and is also undefined behaviour to write), then of course writing the finalZis beyond even any possible NUL and even in C++11 the indexing operation is undefined behaviour, let alone the write....