Using std::string is encouraged. std::string automatically allocates and deallocates storage as needed, can hold strings of arbitrary length, has overloaded operators = and +/+= (for concatenation) and provides a number of other member functions. See here: http://www.cplusplus.com/reference/string/string/
char arrays have no advantages worth speaking of over std::string.
Bud as you where saying char* as a string is a C way of doing it.
In C++ there is no reason not to use strings.
There are some pros to char* but not in a modern computers.
They use less memory and are generally faster is use.
Bud unless your programming microchips or you can't count the age of you computer on your hands and feet there's no reason to use them.
str() returns a temporary string and the pointer c_str() returns is no longer valid when the actual string is destroyed.
So that wouldn't work either.
std::string doesn't necessarily take more memory. While it's true that it usually allocates more than strictly necessary to account for future growth, there's an unholy tendency in C to create char arrays that are as long as the maximum length that the string is ever expected to assume. That doesn't exactly save memory.
itos.str().c_str() does work if you copy the string to a temporary container in the same line.
temporary string will be passed to c_str() and return a const char* to the location still containing the string.
But now that you mention it, it's not 100%.
And people that do char string[MAX_INT] or any of does funny thing I see at the university all the time...
/Slap for being lazy
/Slap for not reading your book
return a const char* to the location still containing the string.
Yes, at that point it still does. However, once the assignment is complete, the temporary string will be destroyed and the location no longer contains anything, i.e. buffer won't point to anything valid (assuming you meant const char* buffer=...).
I mean that is works as long as you know the memory location will not be overwritten. If you know this you can use realloc to make sure it's allocated, but come to think of it, then you need the number of chars too. And if you are going there you can just as well do: