Simple C array declaration / assignment question

后端 未结 9 1880
醉话见心
醉话见心 2021-01-05 06:13

In higher level languages I would be able something similar to this example in C and it would be fine. However, when I compile this C example it complains bitterly. How can

9条回答
  •  抹茶落季
    2021-01-05 06:37

    It is also possible to hide the memcpy by using the compiler's block copy of structs. It makes the code ugly because of all the .i and i: but maybe it solves your specific problem.

    typedef struct {
        int i[3];
    } inta;
    
    int main()
    {
        inta d = {i:{1, 2, 3}};
    
        if (1)
            d = (inta){i:{4, 5, 6}};
    
        printf("%d %d %d\n", d.i[0], d.i[1], d.i[2]);
    
        return 0;
    }
    

提交回复
热议问题