I want to copy a string into a char array, and not overrun the buffer.
So if I have a char array of size 5, then I want to copy a maximum of 5 bytes from a string in
If you always have a buffer of size 5, then you could do:
std::string s = "Your string"; char buffer[5]={s[0],s[1],s[2],s[3],'\0'};
Edit: Of course, assuming that your std::string is large enough.