void main(void)
{
char *p = "name";
p[0] = 's';
printf("%s", p);
}
The above piece of code is giving me eror at run time. The reason for this error might be that I am not allocating any memory for the pointer(correct me if I am wrong).
My doubt is, In the first line of code what exactly is happening.
char *p = "name";
1) Will the compiler places the string "name" in the stack segment of the memory and then initializes p with the address where the string "name" is stored ?
2) Will the string "name" be in the stack???
3) If that is the case why I am getting the error at the run time. The memory is anyhow allocated to the string.
4) I commented out each line and tried debuging. That forced me to the conclusion that the error is in line number 2.
p[0] = 's'; What is the problem with this statement?
If I allocate the memory to the pointer using malloc the error vanishes. Why is it like that??