Code:
char* str = new Char[13];
str = "Hello World!";
With this code you are not making use of your allocated space you created with new. The "Hello World!" string is hard coded into your executable during compile time. When your program is loaded in to memory, along with the hard coded string, the second line justs sets the pointer "str" to the address of the hard coded string. This hard coded string is "const", meaning constant and read only.
Code:
char str[] = "Hello World!";
Please see the above post for this.
Code:
char* str = "Hello World!";
Same as above.
The reason this code is working on the first and last is called undefined behavior. Meaning anything could happen, in this case, the way you to intended it to, but maybe not all the time. That's way its called undefined. And undefined behavior must be avoided at all times.
The proper way for a read only string to be writable is (unfortunately this is C code, not C++, as i do not know how to make a const C string writable with C++, i am pretty certain someone else does around here):
Code:
#include <cstring>
...
const char* src = "this is a constant string.\n";
char* dest = new char[50]; // Allocating enough memory.
std::strcpy(dest, src); // Copy the string to the newly allocated memory.
dest[0] = "T"; // This will work now, since the allocated memory is writable.