0

It's printing 0 but I expect 1. The size of first dimension in array is omitted because I want to pass multiple arrays entries in this same method how to get this for iteration

void printValues(char hotelData[][3][50], int n);

char hotelData [1][3][50] = 

    {
    "unidadeid1Fk", "john fernandes", "executiveRoom"
    };

int main( void )
{
    printValues(hotelData, 0);    
}

void printValues(char hotelData[][3][50], int n)
{        
    int arraySize = sizeof (hotelData) / sizeof (hotelData[0]);
    printf("ArraySize: %i ", arraySize);
}

Output is

 ArraySize: 0

And this code is returning this warning that I don't know what means and how to solve it:

teste.c: In function 'printValues': teste.c:29:32: warning: 'sizeof' on array function parameter 'hotelData' will return size of 'char
(*)[3][50]' [-Wsizeof-array-argument]
         int arraySize = sizeof (hotelData) / sizeof (hotelData[0]);
                                ^ teste.c:27:27: note: declared here
     void printValues(char hotelData[][3][50], int n)
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
JamesB
  • 569
  • 1
  • 9
  • 31
  • I don't know exactly what you want, but an array [decays when passed as a function parameter](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). – Neil May 23 '21 at 02:19
  • [c array size's change when used inside a function](https://stackoverflow.com/q/31797933/995714), [Finding length of array inside a function (duplicate)](https://stackoverflow.com/q/17590226/995714), [How to get array size within function?](https://stackoverflow.com/q/39429730/995714), [Using `sizeof()` on an array passed to a function](https://stackoverflow.com/q/27096272/995714),[Find the Array length inside Function](https://stackoverflow.com/q/34323183/995714), [Why does `sizeof(A)`, for some array A, not work inside a function?](https://stackoverflow.com/q/28651117/995714) – phuclv May 23 '21 at 02:50

1 Answers1

2

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312