Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
These two declarations both represent the visible text code, but they do not create the same kind of value.
std::string word = "code";
char letters[5] = {'c', 'o', 'd', 'e', '\0'};word is a std::string value. Its type represents text and manages the storage needed for that text. letters is a fixed array whose elements happen to be char values. The array does not become a std::string just because its characters spell the same word. Its size and element rules come from the array type.
Which declaration creates a std::string value, and which creates a fixed character array for code?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The four visible characters in letters occupy indices 0 through 3. Index 4 contains '\0', called the null character. It is not another displayed letter. It marks where the C-style text ends.
index: 0 1 2 3 4
value: 'c' 'o' 'd' 'e' '\0'
visible: c o d e endThe fifth slot is why the declaration uses letters[5] for four visible characters. If you leave out '\0', the array still contains characters and can be indexed as an array, but it is not a valid C-style string for operations that search for the terminator. Such an operation may continue reading beyond the array, because it has not found the marker that says the text is finished.
Both values let you reach the character at index 1. For word, word[1] is 'o'. For letters, letters[1] is also 'o'. Updating either element changes that position to 'O', but the two variables still have different types.
word[1] = 'O';
letters[1] = 'O';
word = "code";The final line replaces the whole text stored in word. An array cannot receive a complete array value after its declaration, so an assignment such as letters = {'c', 'o', 'd', 'e', '\0'}; does not restore its contents. You must update its elements one at a time, because the array itself is a fixed group of slots rather than one assignable text value.
Fix the attempted whole-array assignment to letters by restoring c, o, d, e, and the terminator through indexed assignments.
letters = {'c', 'o', 'd', 'e', '\0'};Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For word, the type treats code as one text value, so replacing the stored text with another string value is natural. The object manages the storage associated with that value, rather than exposing a fixed number of character slots that you must preserve yourself.
For letters, the representation is exactly five fixed slots: four for the visible characters and one for '\0'. You must keep every access inside those slots, leave enough capacity for the terminator, and restore each element explicitly when replacing the text. Writing past index 4 is out-of-bounds access, while using all five slots for visible letters would leave no slot for the end marker.