This is a char
:
char c = 't';
It can only hold one char
acter!
This is a C-string:
char s[] = "test";
It can hold multiple char
acters. Another way to write the above is:
char s[] = {'t', 'e', 's', 't', 0};
The 0
at the end is called the NUL
terminator. It denotes the end of a C-string.
A char*
stores the starting memory location of a C-string.1 For example, we can use it to refer to the same array s
that we defined above. We do this by setting our char*
to the memory location of the first element of s
:
char* p = &(s[0]);
The &
operator gives us the memory location of s[0]
.
Here is a shorter way to write the above:
char* p = s;
Notice:
*(p + 0) == 't'
*(p + 1) == 'e'
*(p + 2) == 's'
*(p + 3) == 't'
*(p + 4) == 0 // NUL
Or, alternatively:
p[0] == 't'
p[1] == 'e'
p[2] == 's'
p[3] == 't'
p[4] == 0 // NUL
Another common usage of char*
is to refer to the memory location of a string literal:
const char* myStringLiteral = "test";
Warning: This string literal should not be changed at runtime. We use const
to warn the programmer (and compiler) not to modify myStringLiteral
in the following illegal manner:
myStringLiteral[0] = 'b'; // Illegal! Do not do this for const char*!
This is different from the array s
above, which we are allowed to modify. This is because the string literal "test"
is automatically copied into the array at initialization phase. But with myStringLiteral
, no such copying occurs. (Where would we copy to, anyways? There's no array to hold our data... just a lonely char*
!)
1 Technical note: char*
merely stores a memory location to things of type char
. It can certainly refer to just a single char
. However, it is much more common to use char*
to refer to C-strings, which are NUL
-terminated character sequences, as shown above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…