Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
The number inside a built-in array declaration is its extent, the number of elements the array contains. In standard C++, that extent must be known at compile time. A constexpr variable provides such a value, so this declaration creates exactly four int elements.
constexpr int count = 4;
int scores[count];The array named scores has the same four-element extent for its entire lifetime. You cannot make it grow to five elements, shrink to two, or adopt a new extent by changing count later. A separate variable can control how many positions a loop visits, but changing that variable does not change the storage already reserved for scores.
The declaration reserves space for scores, but it does not by itself place the values 7, 4, 9, and 6 into that space. Those values must be present through initialization or by updating individual elements.
What is the accurate consequence of constexpr int count = 4; int scores[count];?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Because scores is a local array, this declaration reserves four int objects without giving them initial values. Before any update, reading scores[0], scores[1], scores[2], or scores[3] attempts to use an indeterminate value. The declaration is valid, but printing or calculating with any of those four elements at that moment is invalid.
constexpr int count = 4;
int scores[count];
// None of these reads is valid yet.
cout << scores[0];
cout << scores[1];
cout << scores[2];
cout << scores[3];Empty braces change the initialization performed by the declaration. With int scores[count]{}, all four int elements are value-initialized to zero, so each element can be read immediately. This is different from int scores[count];, where the local elements have indeterminate values.
constexpr int count = 4;
int scores[count]{};
cout << scores[0]; // 0
cout << scores[1]; // 0
cout << scores[2]; // 0
cout << scores[3]; // 0You can also make every element contain a known value by updating them before reading them. Updating the four valid positions one by one changes the array to [7, 4, 9, 6]. The array still has four elements, and each value belongs to the position where you stored it.
constexpr int count = 4;
int scores[count];
scores[0] = 7;
scores[1] = 4;
scores[2] = 9;
scores[3] = 6;An extent of four gives scores exactly four positions. Since indexing starts at zero, the valid indices are 0, 1, 2, and 3. After the updates, scores[0] is 7, scores[1] is 4, scores[2] is 9, and scores[3] is 6.
The expression scores[4] does not refer to a fifth safe position. It refers one element past the array. C++ does not automatically produce a bounds error when a built-in array index is invalid. Reading from or writing to scores[4] causes undefined behavior, so the result is not a reliable error, value, or program action.
for (int i = 0; i < count; i++) {
cout << scores[i] << " ";
}
cout << scores[4]; // undefined behaviorRepair the loop condition so this loop fills scores without reaching scores[4].
for (int i = 0; i <= count; i++) {
scores[i] = 7;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A braced list can initialize a built-in array when the array is declared, but it cannot replace the contents of an already declared array through the assignment operator. This declaration is valid because initialization happens as scores is created.
constexpr int count = 4;
int scores[count] = {7, 4, 9, 6};After declaration, the same-looking statement is not a valid whole-array assignment. The name scores identifies the existing array, and a built-in array cannot be assigned another list as one object. The compiler rejects this code before the program runs.
constexpr int count = 4;
int scores[count];
scores = {7, 4, 9, 6}; // does not compileTo change the existing array, update its individual valid elements. For the running array, assigning 7 to scores[0], 4 to scores[1], 9 to scores[2], and 6 to scores[3] changes its contents while preserving the same array identity and fixed extent. Assignment to an element is allowed; assignment to the whole built-in array is not.