I have this code say:
std::string str("ashish");
str.append("\0\0");
printf("%d", str.length());
It is printing 6 but if I have this code
std::string str("ashish");
str.append("\0\0",2);
printf("%d", str.length());
it is printing 8 ! Why?
It’s because
str.append("\0\0")uses the null character to determine the end of the string. So “\0\0” is length zero. The other overload,str.append("\0\0",2), just takes the length you give it, so it appends two characters.From the standard: