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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
A.Robert
  • 304
  • 2
  • 7
  • Possible duplicate of [Variably modified array at file scope](https://stackoverflow.com/questions/4789686/variably-modified-array-at-file-scope) – pipe Mar 22 '18 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. – pipe Mar 22 '18 at 10:26
  • 1
    A more canonical duplicate target may be: *[Variably modified array at file scope in C](https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c)*. – Peter Mortensen Jul 29 '23 at 11:19
  • 2
    Does this answer your question? [Variably modified array at file scope in C](https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c) – Toby Speight Jul 29 '23 at 12:08

0 Answers0