What’s wrong with the following code?
#define DELAY_CYCLES ((int)(0.1/0.001))
typedef struct {
double state_history[N_X][DELAY_CYCLES];
double foo;
} foo
GCC complains:
main.h:52:3: warning: variably modified ‘state_history’ at file scope
Is it because the int cast can’t be done at compile time for some reason?
EDIT AGAIN
If you follow the standard to the letter, then yes, you should avoid floating-point expressions there. In C, with the sole exception of floating-point constants cast to integers (such as
(int)3.0f), floating-point expressions are not considered integer constant expressions at compile time for the purpose of array size calculations. You need to modify the definition to avoid floating-point numbers and use integers exclusively.In order not to be a variable length array, array sizes are required to be “integer constant expressions” (C99 §6.7.5.2/4), and an “integer constant expression” is defined in §6.6/6 (emphasis mine):
It appears that GCC only added that warning in version 4.5. In 4.4 and below, it does not report any warnings for that code, even with
-Wall -Wextra -ansi -pedantic. However, to be safe and 100% portable, you should change the definition ofDELAY_CYCLESto avoid floating-point expressions.