|
|
|
|
|
by dsfuoi
3697 days ago
|
|
This is the first time I have seen sizeof used like this: sizeof( &array[0] )
This looks equal to: sizeof( array )
at first glance, which would give the size of the entire array in bytes, but of course the &array[0] expression is really: &*( array + 0 )
which simplifies to: array + 0
which is a pointer. And using sizeof on it gives the size of a pointer to int.Edit: (&* array) will also give a pointer. --- This is just a really convoluted way to write 2: &array[2] - &array[0]
&*(array+2) - &*(array+0)
(array+2) - (array+0)
2 - 0
Again I have never seen this written in such fashion. |
|
C89 3.3.3.4
"The sizeof operator... When applied to an operand that has array type, the result is the total number of bytes in the array."
C89 3.2.2.1
"Except when it is the operand of the sizeof operator or the unary & operator, or is a character string literal used to initialize an array of character type, or is a wide string literal used to initialize an array with element type compatible with wchar_t, an lvalue that has type ``array of type '' is converted to an expression that has type ``pointer to type '' that points to the initial member of the array object and is not an lvalue."
Additionally, here is a thread of Linus Torvalds pointing out even more of the confusing nature of arrays and sizeof in C:
https://lkml.org/lkml/2015/9/3/428