title: C fixed size array treated as variable size
body: <p>I have been trying to define a static array with size that should be known at compile time (it's a constant expression). It appears that gcc cannot determine the size of the array when it contains a floating point constant (and I get "storage size of ... isn’t constant").</p>
  
  <p>Here is a minimal example:</p>
  
  <pre><code>int main(void)
  {
      static int foo[(unsigned)(2 / 0.5)];
      return 0;
  }
  </code></pre>
  
  <p>What is the reason for this behavior?</p>
  
  <p>EDIT</p>
  
  <p>I already have the answer I needed. I still don't understand the rationale behind not allowing that kind of expressions, but this is a separate question. 
  I'll explain for the curious how I arrived at the problem.</p>
  
  <p>It's about a game I'm writing as an excercise. Units move on a battlefield and I have divided the movement in steps. I have to remember the position of each unit on each step so that I can display animation later. The number of steps is chosen so that it ensures there will be a step on which units are close enough to fight each other but not so close as to collide. Here are the relevant pieces of code:</p>
  
  <pre><code>#define UNIT_SPEED_LIMIT 12
  #define DISTANCE_MELEE 0.25
  #define MOVEMENT_STEPS (unsigned)(2 * UNIT_SPEED_LIMIT / DISTANCE_MELEE)
  
  struct position (*movements)[MOVEMENT_STEPS + 1];
  </code></pre>
  
  <p>Defining <code>DISTANCE_MELEE</code> (maximum distance at which close combat is possible) and using it to calculate the number of steps seems to be the natural way to proceed (more so because I use this constant in multiple contexts). Since I cannot define <code>movements</code> this way, I have to invent a concept like "number of steps for a single unit of distance" and use multiplication by <code>int</code> instead of division by <code>double</code>. I want to avoid dynamic memory allocation in order to keep the code simple.</p>