'Initializer not constant' on global variable?

后端 未结 3 438
无人共我
无人共我 2020-12-20 21:33

So I get the \'initializer element not constant\' error when compiling the following code:

#include 
#include 
#include 

        
3条回答
  •  天命终不由人
    2020-12-20 22:14

    According to C99 standard:

    §6.7.8 Initialization

    1. All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

    Using const doesn't help here because, in C, const variables are not really const. Check out this post for more details.


    To work out, you can make wl constant by using preprocessor:

    #define wl 2.0f
    

    By doing this, 2.0f * (float) M_PI / wl can be a compile time constant.

提交回复
热议问题