if you are using std::string you can simply do array1 = array2; If you are however using a cstring you will need to make sure that the number of elements is within the range of the array you are copying to. You must also append a null-terminator at the end.
What it seems like you are trying to do is something like:
1 2 3 4 5 6 7 8 9 10
std::string array1 = "hello"; //size = 5
std::string array2 = "hi"; //size = 2
for(int i = 0; i < 5; ++i)
{
array2[i] = array[i]; //array 2 is size of 2 so you will soon go out of bounds
}
//you should use this:
array2 = array1;
You must make sure the string you are copying to has room for it. There are several ways to do it. You could call the resize* method array2.resize(elements); then copy how you are. You could set the string to an empty one then append* the characters like:
You could use substr* function and assign that to the other string array2 = array1.substr(0,elements); There are other methods too but these should work for what you want.