When you pass an array in C++, they get passed as a pointer. So we have to pass the size of that array explicitly.
// This will not work.
int GetSize(int* arr){
return sizeof(arr)/size(int);
}
But if we don't use a function to get its size and use "sizeof" in the same function where the array was initialized, now we get the correct result.
int main(){
int arr[5];
int size = sizeof(arr)/sizeof(int);
cout << size << endl;
}
So is it adequate for me to look at the array as a class with a pointer, its size and type of elements in it?
Now that I think about it, I'm not sure how C++ knows array's size in the latter code if it's just handled the same as a pointer.
std::arrayorstd::vectorinstead.int size = sizeof(arr)/sizeof(int);-->const auto size = std::size(arr);