3

Cygwin GCC (4.8.3, 4.9.2) complains about the following code:

unsigned char array[ (unsigned short)((80.0f)/(5.0f)) ];

saying:

ModifiedVariably.c:3:1: error: variably modified 'array' at file scope [-Werror]
unsigned char array[ (unsigned short)((80.0f)/(5.0f)) ];
^

But there isn't any variable, and the result of the floating point calculation is cast to an unsigned before being used as a subscript.

Why are we getting this error, and how (besides not using floating point constants) can it be resolved?

I know that similar questions have been asked before, but as far as I could find out, these involved actually using variables as index, at file scope.

It appears that other compilers (e.g., Visual Studio and the TASKING toolchain) do not have this problem.

P.S.: The actual code has a number of #define's; I took the resulting preprocessor output for simplicity.

4
  • Possible duplicate of Variably modified array at file scope Commented Mar 22, 2018 at 10:25
  • I had the same problem and I agree, most questions here don't actually use constants. But I found another question about actually using floating point constants in the expression, with an answer. I've flagged this for duplication. Commented Mar 22, 2018 at 10:26
  • 1
    A more canonical duplicate target may be: Variably modified array at file scope in C. Commented Jul 29, 2023 at 11:19
  • 2
    Does this answer your question? Variably modified array at file scope in C Commented Jul 29, 2023 at 12:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.