int main() {
char* string = "Hello world";
string[2] = 'd';
printf("%s\n", string);
return 0;
}
int main() {
time_t now;
time(&now);
$
char* time = ctime(&now) + 11;
time[8] = '\0';
printf("%s\n", time);
return 0;
}
Like in the second example I am able to modify a particular index of the array for the pointer to terminate the string with the NULL character. However, with the first one I just try to change the character at index 2 to the letter 'd' but it refuses to print the output and I a Segmentation fault error!
My guess is that in the second one there is some very slight difference where the ampersand is used to get the contents of the address in memory to where the pointer points to but that's just my guess. Would love if someone could give me a clear and not overly confusing explanation as to why this is... Thanks!