how sizeof() works in pass by reference arguments

后端 未结 6 1723
深忆病人
深忆病人 2021-01-22 04:56

I passed a array to function and tried to find the length of the array . but the result was not expected . can anybody explain please?

int main()
{
     int arra         


        
6条回答
  •  臣服心动
    2021-01-22 05:27

    You have to remember that arrays decays to pointers. That means that in the function func the variable arr is not actually an array but a pointer. And doing sizeof on a pointers returns the size of the pointer and not what it points to.

    You get the result 2 because you're on a 64-bit system where pointers are 64 bits (i.e. 8 bytes), an when divided by the size of int (which is 4 bytes) you get the result 2.

提交回复
热议问题