Why isn't the size of an array parameter the same as within main? Why isn't the size of an array parameter the same as within main? arrays arrays

Why isn't the size of an array parameter the same as within main?


An array-type is implicitly converted into pointer type when you pass it in to a function.

So,

void PrintSize(int p_someArray[10]) {    printf("%zu\n", sizeof(p_someArray));}

and

void PrintSize(int *p_someArray) {    printf("%zu\n", sizeof(p_someArray));}

are equivalent. So what you get is the value of sizeof(int*)


It's a pointer, that's why it's a common implementation to pass the size of the array as a second parameter to the function


As others have stated, arrays decay to pointers to their first element when used as function parameters. It's also worth noting that sizeof does not evaluate the expression and does not require parentheses when used with an expression, so your parameter isn't actually being used at all, so you may as well write the sizeof with the type rather than the value.

#include <stdio.h>void PrintSize1 ( int someArray[][10] );void PrintSize2 ( int someArray[10] );int main (){    int myArray[10];    printf ( "%d\n", sizeof myArray ); /* as expected 40 */    printf ( "%d\n", sizeof ( int[10] ) ); /* requires parens */    PrintSize1 ( 0 ); /* prints 40, does not evaluate 0[0] */    PrintSize2 ( 0 ); /* prints 40, someArray unused */}void PrintSize1 ( int someArray[][10] ){    printf ( "%d\n", sizeof someArray[0] );}void PrintSize2 ( int someArray[10] ){    printf ( "%d\n", sizeof ( int[10] ) );}