I can get the sizeof
an object in an array in a local function:
int xa[] = {3,5};
printf("Sizeof: %zu\n", sizeof(xa));
Sizeof: 8
But if I pass it to a function, I get a warning:
void sum_array(int x[]) {
int sum=0;
for (int i=0; i < (sizeof(x) / sizeof(*x)); i++ ) {
sum += x[i];
}
printf("%d\n", sum);
}
'sizeof' on array function parameter ‘x’ will return size of ‘int *’ [-Wsizeof-array-argument]
What's the reason that this occurs? I can suppress this by changing the formal parameter from int x[]
to int* x
, but I'd like to understand why this is occurring. My thought that was within a function parameter that type* x
and type x[]
were identical, but I guess I was mistaken.