C fixed size array treated as variable size

前端 未结 2 615
南方客
南方客 2021-01-17 17:09

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 th

2条回答
  •  [愿得一人]
    2021-01-17 17:41

    OP's primary question is well answer here.

    To address OP's higher level problem of how to use values like 0.5 or 0.25 in pre-processing, use fractional arithmetic:

    #define UNIT_SPEED_LIMIT 12
    // #define DISTANCE_MELEE 0.25
    // use 25/100 or 1/4 or ...
    #define DISTANCE_MELEE_N 1
    #define DISTANCE_MELEE_D 4
    // #define MOVEMENT_STEPS (unsigned)(2 * UNIT_SPEED_LIMIT / DISTANCE_MELEE)
    #define MOVEMENT_STEPS (2u * UNIT_SPEED_LIMIT * DISTANCE_MELEE_D /  DISTANCE_MELEE_N)
    
    struct position (*movements)[MOVEMENT_STEPS + 1];
    

提交回复
热议问题