It applies only to string literals.
A string literal implicitly creates an array object of type char[N]
containing the character in the literal followed by a terminating \0'
character. This object has static storage duration and is read-only. (It's not const
, for historical reasons, but attempting to modify it has undefined behavior.)
When you write:
char *ptr = "abc";
you're creating ptr
as a pointer object, and initializing it to point to the read-only static array containing "abc"
. (You can prevent attempts to modify it by defining it as const
.)
When you write:
char arr[] = "abc";
you're creating arr
as an array object, of type char[4]
, and copying the contents of the static read-only array into that object. arr
is not read-only.
int array[] = {1, 2, 3};
creates an array object and initializes it as shown. There are no "integer array literals" that act like string literals. (There almost are -- see "compound literals" -- but those don't have the same read-only semantics as string literals.)
Note that this:
char t[3] = "abc";
is a special case: if an array is initialized with a string literal, and there isn't room for the terminating '\0'
, then only the non-null characters are copied to the array. As a result, t
does not contain a string, just an unterminated sequence of characters. This isn't particularly relevant to your question.