I've done a bit of basic reading and from what I've gathered .c_str() always has a null terminator.
I have a fairly simple C++ program:
int main(int argc, char** argv)
{
std::string from = "hello";
char to[20];
memcpy(to, from.c_str(), strlen(from.c_str())+1);
std::cout<< to << std::endl;
return 0;
}
Will that memcpy ensure that I copy over a null-terminated string to my variable to (provided that my string from is shorter in length)?
memcpy
doesn't know what is a string. you give it a range to copy. it depends on strlen
here to provide the termination point of range