I'm pretty new to C and there is a concept about strings and pointers I have a hard time to understand. here are three ways to declare a string c :
char *str1 = "Hello world";
char str2[12] = "Hello world";
char *str3 = (char*)malloc(12);
char *(str+5)='\0'; // failure
char str[5]='\0' ; //successfully done
char str[5]= '\0' ; //successfully done
Let's say want to extract the "Hello" substring from the string. I can easily do that by looping through the string until a whitespace is encountred, right?
But here comes the problem :
As I'm trying to assign '\0' or any other character to str1, a write access violation is thrown.
But If I assign '\0' to both str2 and str3, no exception is thrown and the strings are successfully truncated.
I would like to get a clear explanation about why it's impossible to change string values if it's declared like str1.