This problem has been bugging me since forever. I have an array and in the scope of its declaration, I can use the sizeof operator to determine the number of elements in it but when I pass it to a function, it interprets as just a pointer to the beginning of the array and the sizeof operator just gives me the size of this pointer variable. Like in the following example,
#include<iostream>
int count(int a[]){
return (sizeof(a)/sizeof(int));
}
int main(){
int a[]={1,2,3,4,5};
std::cout << sizeof(a)/sizeof(int) << " " << count(a) << std::endl;
return 0;
}
The output of the code is 5 2. How so I pass an array to the function so that I could determine its size by the use of only the sizeof operator and won't have to pass on the extra size as a parameter to this function?
std::array(you'll need a C++11 compiler).boost::array, which is more or less the same thing