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

前端 未结 5 1740
清酒与你
清酒与你 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

    Just for illustration by compare and contrast The code is from http://www.geeksforgeeks.org/g-fact-80/ /The code fails in gcc and passes in g++/

    #include
    int initializer(void)
    {
        return 50;
    }
    
    int main()
    {
        int j;
        for (j=0;j<10;j++)
        {
            static int i = initializer();
            /*The variable i is only initialized to one*/
            printf(" value of i = %d ", i);
            i++;
        }
        return 0;
    }
    

提交回复
热议问题