I\'m a newbie in C++ learning the language and playing around. I wrote a piece of code which behavior I don\'t understand. Could someone explain why the code below prints ou
If your aim is to pass the content of a std::string to a function modifying the content of a char*:
#include
#include
void f(char* s) {
s[0] = 'H';
}
std::vector to_vector(const std::string& s) {
return std::vector(s.c_str(), s.c_str() + s.size() + 1);
}
int main(void)
{
std::string s = "_ello";
std::vector t = to_vector(s);
f(t.data());
std::cout << t.data() << std::endl;
}