C-style strings
An array of characters
In C, we represent a "string" using an array of characters.
To treat the array as an actual string (like "Hello"),
the "null character", '\0', is stored as the last character
in the array to mark the end of the string. Such an
array of null-terminated characters is often referred to
as a "C-style" string.
The presence of the null character is a result of how
the array is used. It's still an arary
of characters.
C-string declarations
char string1[4] = "abc";
char string2[ ] = "abc";
char *stringp = "abc";
// but not this
char string3[ ] = {'a', 'b', 'c'};
C-style string functions
The C library provides several functions to handle and manipulate
C-style strings, including
- strcpy -- copy a C-style string
- strcat -- concatenate two C-style strings
- strlen -- return the length of a C-style string
- strcmp -- compare two C-style strings
Analysis
What are some advantages of C-style strings?
What are some disadvantages of C-style strings?