strPtr1 = charray; //this compiles
charray[0] = strPtr1[0]; //this will compile
charray = &(strPtr1[0]); //wont compile/cannot convert from 'char *' to 'char [20]
}
I was thinking strPtr1 and charray are both just pointers to char arrays, but
then why is it OK to set strPtr1 = charray, but not the other way around?
Is it because if I reset the address which charray[] points to, then the original charray[20] is lost and becomes a memory leak?
charray = &(strPtr1[0]);
What you want to do here is not possible with std::string. Too much risk of buffer overflow.
You can use std::string::c_str(), which returns a const char *. Since it's const, you can't modify its contents. You could cast the pointer it returns to a non-const pointer, but that's not recommendable:
The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.