C Strings - seporaitis/xv6-public GitHub Wiki
C Strings
How are string constants represented internally?
Ref: (Chapter 5.5 Character Pointers and Functions, The C Programming Language)
A string constant is an array of characters. In the internal
representation, the array is terminated with the null character \0
so that programs can find the end. The length in storage is thus one
more than the number of characters between the double quotes.
Perhaps the most common occurrence of string constants is as arguments to functions, as in
printf("hello world\n");
When a character string like this appears in a program, access to it
is through a character pointer; printf
receives a pointer to the
beginning of the character array. That is, a string constant is
accessed by a pointer to its first element.
How are string constants assigned to pointers?
Ref: (Chapter 5.5 Character Pointers and Functions, The C Programming Language)
If pmessage
is declared as
char *pmessage;
then the statement
pmessage = "now is the time";
assigns to pmessage
a pointer to the character array. This is /not/
a string copy; only pointers are involved. C does not provide any
operators for processing an entire string of characters as a unit.
What is the difference between string assigned to array and to pointer?
Ref: (Chapter 5.5 Character Pointers and Functions, The C Programming Language)
There is an important difference between these definitions:
char amessage[] = "now is the time"; /* an array */
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.