as part of a larger task I'm trying to print the length of an array in C, i.e how many elements are in an array. The array is an array of doubles, and I want to print a single integer which is how many doubles are in the array. I have this code:
int array_length( double array[] ) {
double length = 0;
length = sizeof(array) / sizeof(double);
printf("%G", length);
return 0;
}
and I call it in main like this:
int main( void ) {
double test[] = {1.1,2.2,3.3,4.4,5.5};
array_length(test);
return 0;
}
however this returns the error:
error: ‘sizeof’ on array function parameter ‘array’ will return size of ‘double *’ [-Werror=sizeof-array-argument]
research has told me that
sizeof(array) / sizeof(double);
will give me the length of an array, and I am trying to using "%G" to allow me to print the double that this code returns.
I believe that "double*" means long double, so I tried changing "%G" to "%LG" and all uses of "double" to "long double", but that did not solve the problem. What is going on here?