If I want to change the pointed value without changing the pointer's address, what should I do?
In 3, *d=*b
will return segmentation fault in running time. I think it is because the *d
is not pointed to any memory space. This also explains why 2, works. However, for 1, *a
and *b
pointed to memory with same size, why it will return segmentation fault here?
void main(void){
char *a="abcd";
char *b="1234";
char *c=(char *)malloc(sizeof(char));
char *d;
*a=*b; //1.segmentation fault
*c=*b; //2.ok
*d=*b; //3.segmentation fault
}