When an array is passed as an argument to a function, it is automatically converted to a pointer to its first element.1 Similarly, when a declaration of a parameter to a function is an array, it is automatically adjusted to be a pointer.
When the pointer to the first element of the array is passed, no size information is automatically passed. If, inside of the function, you want to know the size of the array, you must pass it as an argument or otherwise make it available to the function.
In contrast, the compiler knows the size of hotelData[0]
because it knows the complete type of hotelData[0]
. While hotelData
has been converted to a pointer, it is a pointer of type “pointer to array of 3 arrays of 50 char
” (also known as char (*)[3][50]
), so it points to an array of 3 arrays of 50 char
, and the size of that is 3•50•1 = 150.
The size of the pointer hotelData
is likely 4 or 8 bytes. In any case, it is certainly less than 150 bytes, so dividing it by 150 yields 0 (in integer arithmetic).
Generally, the compiler only knows the sizes of things whose complete type declarations are visible. Even neglecting the fact that the parameter declaration char hotelData[][3][50]
is automatically adjusted to char (*hotelData)[3][50]
, the complete type of hotelData[][3][50]
is not visible at this point in the source code because the []
is empty. C does not pass any extra information about the size of an object along with the function argument; the type information has to be completely known from the parameter declaration. (Although you and I can see the call to printValues
in the main
routine above, the compiler is not able to use the information about the external object named hotelData
in application to the parameter named hotelData
; they are different things in spite of having the same name.)
Footnote
1 Whenever an array is used in an expression, it is automatically converted to a pointer to its first element unless it is the the operand of sizeof
, is the operand of unary &
or is a string literal used to initialize an array.