A string literal may be used as an initializer in the declaration of either a built-in array of char
s or a variable of type const char *
. The declarations
char color[] = "blue";
const char *colorPtr = "blue";
each initialize a variable to the string "blue"
. The first declaration creates a five-element built-in array color
containing the characters 'b'
, 'l'
, 'u'
, 'e'
and ' '
. The second declaration creates pointer variable colorPtr
that points to the letter b
in the string "blue"
(which ends in ' '
) somewhere in memory. String literals have static storage duration (they exist for the duration of the program) and may or may not be shared if the same string literal is referenced from multiple locations in a program.
Error-Prevention Tip 8.6
If you need to modify the contents of a string literal, store it in a built-in array of chars first.
3.138.125.139