I have been searching about how to get the size of an array decayed as a pointer but most search results seem to explain what decay is and how to prevent it. Below is the code explaining the problem
void print_size(int y[]){
cout<<"sizeof(y) = "<<sizeof(y)<<"\tsizeof(y[0]) = "<<sizeof(y[0])<<"\tnum elements = "<<sizeof(y)/sizeof(y[0])<<endl;
}
main(){
int x[20] = {1,2,3,4};
cout<<"sizeof(x) = "<<sizeof(x)<<"\tsizeof(x[0]) = "<<sizeof(x[0])<<"\tnum elements = "<<sizeof(x)/sizeof(x[0])<<endl;;
print_size(x);
}
The output of the above code is
sizeof(x) = 80 sizeof(x[0]) = 4 num elements = 20
sizeof(y) = 8 sizeof(y[0]) = 4 num elements = 2
The problem in the second output is obviously due to the array decaying into a pointer. Is there any way we can get the size and number of elements in an array after decaying happens?