I've been reading C programming Second edition by B & K. In the Appendix A of the book, where they define the standard reference for C, they say:
"A string literal, also called a string constant, is a sequence of characters surrounded by double quotes as in "...". A string has type ``array of characters'' and storage class static and is initialized with the given characters."
This means that any sequence of characters between double quotes have static storage.
Now, in another part of the book, they say:
"Character arrays are a special case of initialization; a
string may be used instead of the braces and commas notation:
char pattern[] = "ould";
is a shorthand for the longer but equivalent
char pattern[] = { 'o', 'u', 'l', 'd', '\0' };
In this case, the array size is five (four characters plus the terminating '\0')."
In the shorthand statement, does the string "ould" have static storage or not? Or is it just an array of characters as they make it out to be.
Some place later in the book, they also say that:
"There is an important difference between these definitions:
char amessage[] = "now is the time"; /* an array */
and
char *pmessage = "now is the time"; /* a pointer */
amessage is an array, just big enough to hold the sequence of characters and '\0' that initializes
it. Individual characters within the array may be changed but amessage will always refer to the
same storage. On the other hand, pmessage is a pointer, initialized to point to a string constant; the
pointer may subsequently be modified to point elsewhere, but the result is undefined if you try to
modify the string contents."
So, correct me if I am wrong, what I am basically getting from all of this is that:
Once a sequence of characters enclosed in double quotes appear in a program, they stay in memory and aren't destroyed till the end of the program. And also, they are stored in memory as an array of characters, as in char pattern[] = { 'o', 'u', 'l', 'd', '\0' }
. And, when a string appears on the right side of an initialization, like in char pattern[] = "ould";
, the string is copied into the array, character by character into the array's indices, and stays in memory until the end of program as a sequence of characters too regardless of whether pattern[] is destroyed or not.
I am sorry if this is a bit long but it's kinda been confusing me, I've read other answers on this topic on here too but they don't seem to connect well.