In the below simple c++ program, I'm trying to get the length of an array by using sizeof
, for same array, why when passing the array as an argument to a function the array length becomes incorrect? How should I get the length of an array who is an argument of a function?
#include <iostream>
int get_length(int arr[]){
return sizeof(arr)/sizeof(*arr);
}
int main(){
int arr[] = {5,10,15,20};
std::cout << "non function call: " << sizeof(arr)/sizeof(*arr) << std::endl;
std::cout << "function call: " << get_length(arr) << std::endl;
}
running result:
non function call: 4
function call: 2