I know that in C, arrays are not supposed to be dynamically sized.
With that in mind, would the following code be allowed? (Trying to declare an array of chars the same length as a double.)
char bytes[sizeof(double)];
My guess is that sizeof
operates on its argument during program execution and so this wouldn't be allowed, but I'm not sure.
Also, would there be a difference if this were C++ instead of C?
The sizeof expression is evaluated at compile time (by the compiler not the preprocessor) so the expression is legal.
There's an exception to this rule in C99 where dynamic arrays are allowed. In that case sizeof is, depending on context, evaluated at runtime (http://en.wikipedia.org/wiki/Sizeof). It doesn't change the legality of the expression in the question.
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