Is this code well-defined according to the C++ standard?
Is that code ensured by the standard to return a pointer to the first element in the array storing the characters?
Yes and yes.
However, you must realize that the pointed array of chars is not guaranteed to be null terminated. So, you may not pass the pointer to functions that require a null terminated string - unless you set the terminator yourself.
It seems slightly hacky.
It might seem so, but currently this is how to get non-const pointer to the internal array. C++17 will add char* std::string::data()
to make this cleaner.