int n = sizeof(arr)/sizeof(arr[0]);
This line is used to calculate the number of items in the array. sizeof(arr)
gives the total size consumed by the array, and sizeof(arr[0])
gives the size allocated to each element. For example, if it is int so GCC compiler allocation it 4 bytes.
In C, When you pass an array to a function as an argument, C doesn't pass a copy of the whole array, rather it passes the pointer to the first element of the array. So,
fun(arr); // it passes the base address of the array, &arr[0]
So we cannot determine the length of the array inside the called function. If you run sizeof(arr)
inside function fun then it gives the size of the pointer. (which is usually 4 or 8 depending on your platform).
Thus, the right way to pass an array to a function is to pass also the size of the array as a second argument:
fun(arr, n); //arr is array and n is the length of the array
Hope, you understand the concept.