The book C++ Primer states the following:
Because arrays are passed as pointers, functions ordinarily don't know the size of the array they are given. They must rely on additional information provided by the caller.
The options described include using an end marker character, using a pointer to the position one pas the last element of the array, or using a parameter to represent the size of the array.
However, when I create a non-null terminated array of const char
and pass that to a function called print
, the <<
operator knows perfectly when to stop reading without any of these techniques. How does it know?
#include <iostream>
void print(const char line[]) {
std::cout << line << '\n';
return;
}
int main() {
const char str[] {'f', 'o', 'o', ' ', 'b', 'a', 'r'};
print(str);
return 0;
}