Character sequences - C++ Tutorials
Character sequences - C++ Tutorials
Search: Go
Not logged in
C++
Information
Tutorials
Reference
Articles
Forum
The above declares an array of 6 elements of type char initialized with the characters that form the word "Hello" plus a
null character '\0' at the end.
But arrays of character elements have another way to be initialized: using string literals directly.
In the expressions used in some examples in previous chapters, string literals have already shown up several times.
These are specified by enclosing the text between double quotes ("). For example:
Sequences of characters enclosed in double-quotes (") are literal constants. And their type is, in fact, a null-terminated
array of characters. This means that string literals always have a null character ('\0') automatically appended at the
end.
Therefore, the array of char elements called myword can be initialized with a null-terminated sequence of characters by
either one of these two statements:
In both cases, the array of characters myword is declared with a size of 6 elements of type char: the 5 characters that
compose the word "Hello", plus a final null character ('\0'), which specifies the end of the sequence and that, in the
second case, when using double quotes (") it is appended automatically.
Please notice that here we are talking about initializing an array of characters at the moment it is being declared, and
not about assigning values to them later (once they have already been declared). In fact, because string literals are
regular arrays, they have the same restrictions as these, and cannot be assigned values.
Expressions (once myword has already been declared as above), such as:
1 myword = "Bye";
2 myword[] = "Bye";
https://www.cplusplus.com/doc/tutorial/ntcs/ 1/2
6/3/2021 Character sequences - C++ Tutorials
This is because arrays cannot be assigned values. Note, though, that each of its elements can be assigned a value
individually. For example, this would be correct:
1 myword[0] = 'B';
2 myword[1] = 'y';
3 myword[2] = 'e';
4 myword[3] = '\0';
In the standard library, both representations for strings (C-strings and library strings) coexist, and most functions
requiring strings are overloaded to support both.
For example, cin and cout support null-terminated sequences directly, allowing them to be directly extracted from cin
or inserted into cout, just like strings. For example:
In this example, both arrays of characters using null-terminated sequences and strings are used. They are quite
interchangeable in their use together with cin and cout, but there is a notable difference in their declarations: arrays
have a fixed size that needs to be specified either implicit or explicitly when declared; question1 has a size of exactly
20 characters (including the terminating null-characters) and answer1 has a size of 80 characters; while strings are
simply strings, no size is specified. This is due to the fact that strings have a dynamic size determined during runtime,
while the size of arrays is determined on compilation, before the program runs.
In any case, null-terminated character sequences and strings are easily transformed from one another:
Null-terminated character sequences can be transformed into strings implicitly, and strings can be transformed into
null-terminated character sequences by using either of string's member functions c_str or data:
Previous: Next:
Arrays Pointers
Index
https://www.cplusplus.com/doc/tutorial/ntcs/ 2/2