Please take a look at the minimal code example below. I've noticed a behavior in C that I cannot find an explanation for, so I decided to post a question here.
Case 1
The goal was to declare and initialize an array in main
, and then pass it to function1
. The trick was to then find the number of elements of that array in function1
without having to first calculate it in main
, and then pass it as the argument. That did not work.
Case 2 and Case 3
Here, I calculate the number of elements in the array in main
, and then pass it as the argument in function2
. That works perfectly.
Question
My question is not necessarily why this process of calculating the number of elements in an array does not work in functions other than main
; that kind of makes sense.
My understanding is that the array is nothing but the pointer to the first element (in this case double *
). The size of such pointer (at least on my computer architecture) is 8 bytes. So, sizeof(a) = 8
. This is also the same as size of any one of the elements (sizeof(a) == sizeof(a[0])
). Thus, I will always get that numOfElements == 1
.
My question is rather what makes main
so special that it knows how to "properly" answer my query regarding sizeof(a)
. Particularly, since I had an array of 4 doubles, main will tell me that sizeof(a) == 32
, which is exactly what I needed to calculate numOfElements
.
Also, why is GCC giving me the warning? Does it know that I will not get the value I am actually looking for?
Thank you to anyone who advances my knowledge. I apologize if the answer is rather obvious. Perhaps, I am looking at this from a bit too abstract point of view than needed to analyze this behavior.
Code
#include <stdio.h>
void function1(double a[])
{
int numOfElements = sizeof(a) / sizeof(a[0]);
printf("\n ---> Size of array in function1: %d\n", sizeof(a));
// Some operations...
}
void function2(double a[], int numOfElements)
{
// Some operations...
}
int main()
{
double a[] = {1.32, 2, 5.52, 9.99};
// Case 1
function1(a);
// Case 2
int numOfElements = sizeof(a) / sizeof(a[0]);
function2(a, numOfElements);
// Case 3 - a more compact version of Case 2
function2(a, sizeof(a) / sizeof(a[0]));
printf("\n ---> Size of array in main: %d\n", sizeof(a));
return 0;
}
Output and Warning