C2212: Initializer may be not constant

[ERROR]

Description

A global variable was initialized with a non-constant.

Example
  int i;

  
  int j=i;  // error

  
  or

  
  void function(void){

  
    int local;

  
    static int *ptr = &local;

  
    // error: address of local can be different

  
    // in each function call.

  
    // At second call of this function *ptr is not the same!

  
  }

  
Tips

In C, global variables can only be initialized by constants. If you need non-constant initialization values for your global variables, create an InitModule() function in your compilation unit, where you can assign any expression to your globals. This function should be called at the beginning of the execution of your program. If you compile your code with C++, this error won't occur anymore! In C, initialization of a static variables is done only once. Initializer is not required to be constant by ANSI-C, but this behavior will avoid troubles hard to debug. You can disable this error if your initialization turns out to be constant

See also