Possible Duplicate:
Why is this C code causing a segmentation fault?
char* string = "abcd";
now when i try to change some character of this string i get segmentation fault
*string = 'p';
or
string[0] = 'p';
string[0] = 52;
Can someone please explain me the reason that why is it happening.
Thanks
Alok.Kr.
If you write
char* string = "abcd";the string “abcd” is stocked into the static data part of your memory and you can’t modify it.And if ou write
char* string = 'p';, that’s just wrong. First, you try to declare a variable with the same name (string) and, worse, you try to assign a char value to a char pointer variable. This doesn’t work. Same thing :char[0] = 'p';really means nothing to your compiler except a syntax error.