
Notes—Chap. 7c Arrays and Strings (pp. 297 – 303,
and Appendix D)
- This section introduces the defined type string.
- Can work with character strings in C++ in two ways:
- as Cstrings, i.e., character arrays
- as objects of the class string, whose definition is provided when you
#include <string>
- Quoted strings in C++ are represented as an array of characters terminated
by a null character, i.e., \0, which is the byte containing all 0 bits.
- Example: "ABC" is stored as 4041420016
- A "Cstring variable" named s1 can be declared in any of the following ways:
char s1[] = "ABC";
char s1[4] = "ABC"
char s1* = "ABC";
- Functions are available in the cstring library to manipulate Cstrings, e.g.,
char s2[10];
...
strcpy(s2, s1); // copies s1 into s2
- s2 is now: 40414200????????????16
- Using Cstrings is the more primitive way of handling strings; using objects of
type string is better (and is so done by the textbook).
- Example:
string s1 = "ABC";
string s2;
...
s2 = s1; // copies s1 into s2
- See Appendix D for string constructors, functions, and operators.