Possible Duplicate:
Modifying value of char pointer in c produces segfault
This is a piece of code …
void main()
{
char *p="Hello";
*p= 'h'; // Segmentation fault .
}
I understand the fact that there is a segmentation fault and it gives me a run time error also .But I wonder , why is it a RUN TIME ERROR ?? Why cant the compiler tell me before executing the program ? Why does not it show a COMPILE TIME ERROR ?
PS : I use Visual C++ 2005 Express ..
String literals are really of type
char const*. However, for compatibility with older C code that’s not const-correct, C++ allows them to be assigned to achar*. That does not mean you are really allowed to modify them.