#include <iostream>
#include <sstream>
template <typename T>
const char* numberToString(T number) {
    std::ostringstream ss;
    ss << number;
    return ss.c_str();
}
int main() {
    printf(numberToString(123));
    return 0;
}
My error:
1>d:\programming\euler\problem 4\problem 4\problem 4\source.cpp(8): error C2039: 'c_str' : is not a member of 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
1>          d:\programming\euler\problem 4\problem 4\problem 4\source.cpp(26) : see reference to function template instantiation 'const char *numberToString<int>(T)' being compiled
1>          with
1>          [
1>              T=int
1>          ]
Why doesn't this work?

const char*( c_str() ) from a temporary string. Once you return from the numberToString yourstringstreamwill be destroyed and its underlyingstd::stringwill be destroyed also. When you invokec_str()it will return a pointer to something that is valid only through thestd::stringlife. I suggest working withstd::string, since it is a object (not a pointer) it will be copied when needed.