When I use this code in the main()
function, the size (or length) of the array is calculated correctly:
#include <stdio.h>
int main(void)
{
int arr[] = {2, 5, 9, 8, 4, 5, 1, 2, 8, 5};
int length = sizeof(arr) / sizeof(arr[0]);
printf("The number of elements in the array is %d\n", length);
return 0;
}
Output:
The number of elements in the array is 10
But when I do the same thing by using a function, then the output is wrong:
#include <stdio.h>
int sizeTeller(int array[]);
int main(void)
{
int arr[] = {2, 5, 9, 8, 4, 5, 1, 2, 8, 5};
printf("The number of elements in the array is %d\n", sizeTeller(arr));
return 0;
}
int sizeTeller(int array[])
{
int len;
return (len = sizeof(array) / sizeof(array[0]));
}
Output:
The number of elements in the array is 2
Can you please explain this to me that why this is so? I prefer to keep all the codes in functions that's why I tried the same here but the output went wrong.
This is because, like for many other casesNOTE, when arrays are passed as a function argument, they decay to the pointer to the first element of the array. Hence, in the called function, the received parameter type is a pointer, not an array.
So, in case of
int sizeTeller(int array[])
{
int len;
return (len = sizeof(array) / sizeof(array[0]));
}
is equivalent as
int sizeTeller(int* array)
{
int len;
return (len = sizeof(array) / sizeof(array[0])); // sizeof (int *) / sizeof (int)
}
Solution: If you need to pass an array as a function argument, and need to know it's size, you need to calculate the size in the caller and pass that as another argument to the called function.
NOTE:
Quoting C11
, chapter 6.3.2.1/P3
Except when it is the operand of the
sizeof
operator, the_Alignof
operator, or the unary&
operator, or is a string literal used to initialize an array, an expression that has type 'array of type' is converted to an expression with type 'pointer to type' that points to the initial element of the array object and is not an lvalue. [....]