C arrays and std::array
Five scores can be stored in five variables, but they cannot be added up in a loop: each has its own name. An array stores several values of one type under one name, and each value is reached by its position:
#include <iostream>
int main() {
int scores[5] = {90, 75, 60, 85, 70};
int sum = 0;
for (int s : scores) {
sum = sum + s;
}
std::cout << scores[0] << ' ' << scores[4] << '\n'; // 90 70
std::cout << sum << '\n'; // 380
}int scores[5] declares an array of five int values that sit next to each other in memory. Elements are numbered from zero, so the last one is scores[4]. The range-for loop visits all five elements.
Size belongs to the type
The number in square brackets belongs to the type: scores has the type int[5], and int[3] is a different type. That is why the size must be a value known at compile time, such as a literal or a constexpr variable. sizeof(scores) gives the size of the whole array in bytes, and dividing it by one element's size gives the element count:
int scores[5] = {90, 75, 60, 85, 70};
std::cout << sizeof(scores) / sizeof(scores[0]) << '\n'; // 5A C array does not check its bounds. Accessing a position outside it, such as scores[5], is undefined behavior.
An array as a parameter
A C array cannot be copied into a function parameter. Instead, the function receives a pointer to the first element—this is called array-to-pointer decay. The parameter int a[5] means the same as int* a, and the compiler ignores the number in the brackets.
So inside the function sizeof(a) gives the size of a pointer, not of the array, and says nothing about the length. The length is passed to the function as a separate parameter, like n in the pointer exercises.
std::array
std::array<int, 5> from <array> stores the same five int values in a row, yet is copied and passed whole, like an int. It does not turn into a pointer, and size() returns the number of elements. It is passed by const reference, like other objects that are expensive to copy.
#include <array>
#include <iostream>
int total(const std::array<int, 5>& values) {
int sum = 0;
for (int v : values) {
sum = sum + v;
}
return sum;
}
int main() {
std::array<int, 5> scores = {90, 75, 60, 85, 70};
std::cout << scores.size() << ' ' << total(scores) << '\n'; // 5 380
}The angle brackets hold the element type and the number of elements. Here, too, the size is part of the type, so total accepts only an array of exactly five int values.
Common mistake
The number of elements is computed inside a function:
int count(int a[5]) {
return sizeof(a) / sizeof(a[0]); // ✗
}warning: sizeof on array function parameter will return size of 'int *' instead of 'int[5]' [-Wsizeof-array-argument]This is a warning, not an error: the function compiles, but a inside it is a pointer. sizeof(a) gives the size of a pointer, and the result does not depend on the array's length. Pass the length as a second parameter or use std::array.