Why does the code below give me a segmentation fault.
int main()
{
char *something = "hello\n";
something[2] = 'a'; // doesn't work
*(something+2) = 'a'; // this doesn't work either
return 0;
}
But the following code works fine.
int main()
{
char something[] = "hello\n";
something[2] = 'a'; //works fine
*(something+2) = 'a'; //works fine
return 0;
}
The "something" variable is a char array in both so why can't I assign a character literal to the third element of the array in the first example?