Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
int main()
{
char *c = "abc";
*c = 'd';
printf("%s",c);
return 0;
}
When I tried to run this program in C then the program crashes..I want to know what is the error here?
Because the string literal
abcis actually stored in a read-only area of the process and you are not supposed to modify it. The operating system has marked the corresponding pages as read-only and you get a runtime exception for an attempt to write there.Whenever you assign a string literal to a
charpointer, always qualify it asconstto make the compiler warn you about such problems:If you really want to modify a string literal (although not directly itself, but its copy), I would suggest using
strdup: