Both those code snippets are undefined behavior.
In the first, the pointer word
is uninitialized, so its value is indeterminate (and will seem to be random), so when using to write data you don't know where it will be written.
The second will always write to address zero, which is also undefined behavior.
The solution to this is to remember that you are using C++, which have std::string
:
std::string word;
word = "12";
Or if you have the number as an integer that you want to use, then look at std::ostringstream
:
int value = 12;
// ...
std::ostringstream os;
os << value;
std::string word = os.str();