Just for illustration/comparison (in addition to the answers given already): the counter example...
const char *name1 = "Alex";
int main()
{
name1[0] = 'J';
name1[1] = 'o';
name1[2] = 'h';
name1[3] = 'n';
std::cout << name1 << std::endl;
return 0;
}
Now you do attempt to really modify the immutable string. Luckily, the compiler detects it and prevents you from doing so! const_cast<char>(name[x]) = y;
would be next attempt. Looks like fooling the compiler, actually, you fool yourself only by lying and in consequence running into undefined behaviour!
Actually, casting const away is almost always a bad idea (but is legal, if and only if the pointer holds the address of some originally non-const
ly created memory).