This simple code:
#define WIDTH 500.5
#define NB 23.2
int x[(int)(WIDTH/NB)];
gives me a warning:
prog.c:4:1: warning: variably modified 'x' at file scope [enabled by default]
If I set #define WIDTH 500
and #define NB 23
, the warning disappears.
Passing a float value for WIDTH
macro forces evaluation by the compiler and thus issues a warning because array has not a constant size.
The preprocessed C code looks like int x[(int)(500.5/23.2)];
, whereas int x[(int)(500/23)];
is OK for the compiler (value is already constant integer)
I would like to find a way either
-Werror
: seems that is a lost cause: GCC, C: Finding out name of default warnings for use in #pragma ignore
Funny thing: compiling with g++
I don't have the warning whereas I read here that variable length arrays are not officially supported in C++, only in C99. But that's not an option for me, since I need to stick to C.
It just violates the standard:
Integer constant expression
An integer constant expression is an expression that consists only of operators other than assignment, increment, decrement, function-call, or comma, except that cast operators can only cast arithmetic types to integer types, integer constants, enumeration constants, character constants, floating constants, but only if they are immediately used as operands of casts to integer type
And further:
The following contexts require expressions that are known as integer constant expressions':
...
- The index in an array designator (since C99)
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