Friday, 23 June 2017
c - Why does sizeof(my_arr)[0] compile and equal sizeof(my_arr[0])?
Answer
Answer
Why does this code compile?
_Static uint32_t my_arr[2];
_Static_assert(sizeof(my_arr) == 8, "");
_Static_assert(sizeof(my_arr[0]) == 4, "");
_Static_assert(sizeof(my_arr)[0] == 4, "");
The first 2 asserts are obviously correct, but I would have expected the last line to fail, as my understanding is that sizeof()
should evaluate to an integer literal, which can't be treated as an array. In other words, it would fail in the same way that the following line fails:
_Static_assert(4[0] == 4, "");
Interestingly, the following does indeed fail to compile (which should be doing the same thing, no?):
_Static_assert(*sizeof(my_arr) == 4, "");
error: invalid type argument of unary '*' (have 'long unsigned int')
_Static_assert(*sizeof(my_arr) == 4, "");
If it matters, I'm using gcc 5.3.0
Answer
sizeof
is not a function. It's a unary operator like !
or ~
.
sizeof(my_arr)[0]
parses as sizeof (my_arr)[0]
, which is just sizeof my_arr[0]
with redundant parentheses.
This is just like !(my_arr)[0]
parses as !(my_arr[0])
.
In general, postfix operators have higher precedence than prefix operators in C. sizeof *a[i]++
parses as sizeof (*((a[i])++))
(the postfix operators []
and ++
are applied to a
first, then the prefix operators *
and sizeof
).
(This is the expression version of sizeof
. There's also a type version, which takes a parenthesized type name: sizeof (TYPE)
. In that case the parens would be required and part of the sizeof
syntax.)
casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV
In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...
-
Parse error: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' ...
-
The whole error output being: LNK2019 unresolved external symbol wWinMain referenced in function "int __cdecl __scrt_common_main_seh(vo...
-
I recently used canvas to conert images to webp, using : const dataUrl = canvas.toDataURL('image/webp'); But this takes a lots of ti...
No comments:
Post a Comment