#include <stdio.h>
void dosmth(char* ptr, char* arr);
int main() {
char* ptr = "hello";
char arr[] = "jumbo";
dosmth(ptr, arr);
return 0;
}
void dosmth(char* ptr, char* arr) {
arr[0] = 'J';
ptr[0] = 'H'; //works if line is commented out
printf("%s\n", arr);
printf("%s\n", ptr);
}
My understanding is that char* and char[] are different types. The former is a pointer to an array of characters. This initialization above creates a constant string literal that lives in the read-only part of the memory which cannot be changed at any time.
The latter is an incomplete array of a certain number of characters, but that can be changed.
However, I don't understand why, after the array has decayed to a pointer when passed as an argument, it is still possible to change for example arr[0], yet (as expected) not ptr[0]?