C const char * vs char *
The difference between const char *str and char *str is the level of protection provided for the string stored at the memory location str points to.
const char *str is a pointer to a constant character. This means that the string stored at the memory location str points to cannot be modified through str. Attempting to modify the string will result in a compile-time error.
char *str is a pointer to a character that is not constant. This means that the string stored at the memory location str points to can be modified through str.
int main() {
const char* str = "hello";
*str = 'H';
return 0;
}
$ gcc test.c
test.c:3:8: error: read-only variable is not assignable
*str = 'H';
~~~~ ^
1 error generated.
In general, it is good practice to use const char *str when passing strings to functions to protect the original data from being modified.
Read other posts