Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable this particular warning

Tags:

c

gcc

warnings

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

  • to ignore this particular warning (but leaving the others so I can enable -Werror: seems that is a lost cause: GCC, C: Finding out name of default warnings for use in #pragma ignore
  • fix the code so it does what I want without issuing warnings.
  • force the pre-processor to perform the computation as integer

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.

like image 429
Jean-François Fabre Avatar asked Sep 15 '16 12:09

Jean-François Fabre


1 Answers

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)
like image 93
Serge Avatar answered Sep 18 '22 10:09

Serge