The below code will create a Pointer to a string literal that will be in read only memory(non mutable as the book said)
I tried it and indeed I got a seg fault trying to write to it
char *MyString = "Why won't you write to me?";
Also why would anyone create a string literal like that?
There are all kinds of reasons... Error messages, Titles, Help text and so on. The advantage here is that by making a pointer the text exists only once in memory and the pointer only uses 4 ( or 8 ) bytes of memory on the stack... if you use it to intialize a mutable array, it's in memory twice.
ok now the code below will create a char array but still a string literal
char AnotherString[] = "This is another string";
The above code is still read only correct?
No it isn't... What you are actually doing is creating a char array, the length of your initializer. As long as you are mindful of the length you can make changes to this. The advantage is that you can change it, but the disadvantage is that your string now exists twice in memory; once as the constant initializer and again in the array; using the length of the string plus the size of the pointer of space.
For example you could use
memcpy(AnotherString, "That", 4) and the result would be "That is another string"
Finally if you want to make that immutable you would use
const as in
const char String[] = {"this is a test"}; The below code creates a char array and is writable (mutable as the book said)
char MyMutableArray[256] = "this is a writeble char array";
Yes, this can be changed by any of the library's string functions (check string.h in the help file).
Just trying to understand how C handles all this and don't want to move forward until I feel I have a better understand of C char arrys
You can safely move forward.
Remember C does not have strings... a char array is just an array, like any other. A char is simply an 8bit binary number that happens to produce certain recognizeable shapes when sent to the display (Check the ascii chart in the help file). The only thing special about char strings is the terminating null (\0) that library functions count on to know when to stop.