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