Error “initializer element is not constant” when trying to initialize variable with const

前端 未结 5 1738
清酒与你
清酒与你 2020-11-21 13:02

I get an error on line 6 (initialize my_foo to foo_init) of the following program and I\'m not sure I understand why.

typedef struct foo_t {
    int a, b, c;         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 13:56

    In C language, objects with static storage duration have to be initialized with constant expressions, or with aggregate initializers containing constant expressions.

    A "large" object is never a constant expression in C, even if the object is declared as const.

    Moreover, in C language, the term "constant" refers to literal constants (like 1, 'a', 0xFF and so on), enum members, and results of such operators as sizeof. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type.

    For example, this is NOT a constant

    const int N = 5; /* `N` is not a constant in C */
    

    The above N would be a constant in C++, but it is not a constant in C. So, if you try doing

    static int j = N; /* ERROR */
    

    you will get the same error: an attempt to initialize a static object with a non-constant.

    This is the reason why, in C language, we predominantly use #define to declare named constants, and also resort to #define to create named aggregate initializers.

提交回复
热议问题