在 c 中初始化结构成员的联合时出错

问题描述 投票:0回答:1

我有这样的结构 -

struct ArrayAdv{
        int size;
        int length;
        union{
                struct{
                        int *A;
                } dynamic;
                struct{
                        int A[20];
                } stat;
        } item;

};

当我尝试初始化数组时出现错误 -

Error I am encountered with

我正在尝试像这样初始化数组 -

struct ArrayAdv arrAdv;
arrAdv.item.stat.A = {33, 2, 7, 88, 35, 90, 102, 23, 81, 97};

收到我提到的错误,我希望它被正确初始化。

arrays c initialization structure union
1个回答
1
投票

你得到一个错误,因为你实际上不是initializing,而是assigning,你不能分配给一个数组。初始化发生在定义变量时。

您要找的是:

struct ArrayAdv arrAdv = 
    { .item = { .stat = { .A = { 33, 2, 7, 88, 35, 90, 102, 23, 81, 97 } } } };
© www.soinside.com 2019 - 2024. All rights reserved.