Yes, it's necessary to send the size always, sizeof(arr)
reports the size of a pointer:
Q: Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? I have a test routine
f(char a[10]) { int i = sizeof(a); printf("%d\n", i); }
and it prints 4, not 10.
A: The compiler pretends that the array parameter was declared as a pointer (that is, in the example, as char *a; see question 6.4), and
sizeof reports the size of the pointer. See also questions 1.24 and
7.28.
References: H&S Sec. 7.5.2 p. 195
http://c-faq.com/aryptr/aryparmsize.html
Or you can use an extra first element containing the size:
#include <stdio.h>
void length(int arr[])
{
int len = arr[-1];
printf("%d", len);
}
int main(void)
{
int arr[]={0,4,5,6,3,21,9,3,8}; /* Note first element = 0 */
arr[0] = sizeof(arr) / sizeof(arr[0]) - 1;
length(arr + 1);
return 0;
}