Every one know stringstream.str() need a string variable type to store the content of stringstream.str() into it .
I want to store the content of stringstream.str() into char variable or char array or pointer.
Is it possible to do that?
Please, write a simple example with your answer.
Why not just
?
Edit: Note that you cannot freely give the
poutside your function: its lifetime is bound to the lifetime ofs, so you may want to copy it.Edit 2: as @David suggests, copy above means copying of the content, not the pointer itself. There are several ways for that. You can either do it manually (legacy way “inherited” from C) — this is done with the functions like
std::strcpy. This way is quite complicated, since it involves manual resources management, which is usually discouraged, since it leads to a more complicated and error-prone code. Or you can use the smart pointers or containers: it can be eitherstd::vector<char>orstd::unique_ptr/std::shared_ptr.I personally would go for the second way. See the discussion to this and @Oli’s answer, it can be useful.