繁体   English   中英

如何在另一个struct arrya中的一个结构中初始化一个int数组?

[英]How to initialize an int array within a struct within another struct arrya?

struct customFunction {
    int id;
    int numOfSubfunctions;
    int subfunctions[];
};

const customFunction supportedFunctions[] = {
    {
        0x01,
        1,
        {
            0x01
        }
    },
    {
        0x02,
        2,
        {
            0x01,
            0x02
        }
    }
    ...
};

supportedFunctions数组将用于检查将来是否支持特定功能,并用于标识要使用的功能等。

目前,我出现此错误:

“ int [0]”的初始化程序太多

指向

{
    0x01
}

对于任何功能,子功能可能有0 - n

这将永远行不通。 同一数组中的多个customFunction元素的customFunction subfunctions[]成员不能具有不同的大小。 数组元素的大小必须相同。

如果customFunction元素需要具有不同大小的customFunction subfunctions[]数组,则必须将实际数组存储在内存中的其他位置,然后指向它们,例如:

struct customFunction {
    int id;
    int numOfSubfunctions;
    const int *subfunctions;
};

const int subfunctions_1[] = {
    0x01
};

const int subfunctions_2[] = {
    0x01,
    0x02
};

const customFunction supportedFunctions[] = {
    {
        0x01,
        1,
        subfunctions_1
    },
    {
        0x02,
        2,
        subfunctions_2
    }
    ...
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM