Back to questionsWhat is
JuniorTheoryVery common
What is sizeof(a) equal to inside print_size?
#include <iostream>
void print_size(int a[5]) {
std::cout << sizeof(a) << '\n';
}
int main() {
int a[5] = {1, 2, 3, 4, 5};
print_size(a);
}sizeof(int*)
- Expecting
5 * sizeof(int): a parameter declared asint a[5]is reallyint* aand the5is ignored, sosizeof(a)gives the size of a pointer. - Expecting
5, the number in the brackets:sizeofmeasures bytes, not elements, and hereais a pointer, so the result issizeof(int*).
- How can
print_sizefind out how many elements are in the array it receives? - Why does a
std::array<int, 5>keep its size when it is passed to a function?