So I have this super simple C code:
void reverse_string(char* string) {
int length = strlen(string);
for (int i = 0; i < length / 2; i++) {
char temp = string[i];
string[i] = string[length - 1 - i];
string[length - 1 - i] = temp;
}
}
int main() {
// char* string = "hey this is a string"; <-- bus error
char string[] = "hey this is a string";
reverse_string(string);
printf("%s\n", string);
return 0;
}
and when I use the commented out line I get a bus error, but when I use the line below it everything's fine. Why is that?