sizeof arr
evaluates to the size of arr
in bytes. Because arr
is an array of four four-byte int
, its size is 16 bytes.
sizeof crr
evaluates to the size of crr
. Because crr
is an eight-byte pointer, its size is eight bytes.
You may be accustomed to the fact that arrays are often automatically converted to pointers. When an array is passed to a function, it is converted to a pointer. When an array is used in an index expression, such as arr[2]
, it is converted to a pointer (and then the index is added to it with pointer arithmetic, and the resulting pointer is dereferenced, as if it had been written *(arr+2)
). This automatic conversion does not occur with sizeof
. C 2018 6.3.2.1 3 says:
Except when it is the operand of the sizeof
operator, or the unary &
operator, or is a string literal used to initialize an array, an expression that has type “array of type” is converted to an expression with type “pointer to type” that points to the initial element of the array object and is not an lvalue…