I found it strange, that I am getting compile error when using sizeof int, while sizeof var (where var is a variable) works just fine.
int a;
a = (int) sizeof( a ); //ok
a = (int) sizeof( int ); //ok
a = (int) sizeof a; //ok
a = (int) sizeof int; //error
Can anyone tell me, why do I get the error?
(older machine, mac osx 10.5.8, gcc 4.0.1)
According to the C standard (cited from ISO/IEC 9899:TC3 section 6.5.3.4):
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.
So using a type name without parenthesis isn't legal.
Also, sizeof returns an implementation defined value of the type size_t and you probably should not cast it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With