banner
cells

cells

为美好的世界献上 code

Q206

Q206#

#include <iostream>

int main(int argc, char *argv[]) {
    int n = sizeof(0)["abcdefghij"];
    std::cout << n;
}

Answer#

click to see answer 1

Explain#

We have several pieces of the puzzle, so let's peel away the layers.

unary-expression:
...
sizeof unary-expression
sizeof ( type-id )
sizeof ... ( identifier )
...

We have three cases and the one that applies here is sizeof unary-expression. The unary expression is (0)["abcdefghij"], which looks odd but is just array indexing of string literal which is a const char array.

The expression E1[E2] is identical (by definition) to *((E1)+(E2))

So we end up with 0th element of "abcdefghij", which is a, which is a char. And the result of sizeof('a') will be 1

... sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.