How to create a function to get the size of an array in C? Function and array aren't from the same scope. for example:
int func(int arr[]);
int main(){
int arr[5];
}
int arr_length(int arr[]){
int length = sizeof(arr)/sizeof(int);
return length;
}
The problem with the function above that it treats arr as a pointer so it has the size of a pointer
arr_length, thearrparameter is a pointer. In C, the way is to pass the length along with the pointer:int arr_length(int arr[], size_t arr_size) { return arr_size; }int func(int arr[]);is really the same asint func(int *arr);malloc_usable_size, or other things that are only for dynamically allocated memory. If not, what platform provides a way forfuncto get the size associated with itsarrparameter when the caller definedint a[3][5]and passeda[1]?size_tparameter?intcannot portably represent all possible sizes.