Character pointer to string

Nov 12, 2014 at 3:37pm
When i declare char* szString = "Hello"; and then do the following, szString[0] = 'J';, and then try to print cout << szString; nothing prints, why is that? :)

Is it because i changed one of the characters, or it was not declared const?
Last edited on Nov 12, 2014 at 3:39pm
Nov 12, 2014 at 4:00pm
Because trying to modify string literal is undefined behavior. Often it leads to crash. Sometimes it is not. Sometimes nothing happens. Sometimes it works as expected. Sometimes it changes all similar literals. Sometimes something else happens.

Making char* point to string liteals is deprecated in modern C++. Correct type for it is const char* (or, better, use std::string)
Topic archived. No new replies allowed.