我已经搜索了这么多,但还没有找到c的任何好的解决方案。我想初始化一个具有动态大小的数组。到目前为止,我看到的解决方案是一个链表,但它似乎不符合我的要求。
到目前为止,我测试过的代码如下
typedef struct
{
const unsigned char widgetCount;
unsigned char index;
lv_obj_t * radiobtnVIEW[widgetCount];
}AssetVIEW;然后初始化widgetCount
AssetVIEW View={4};我得到的错误是
error: 'widgetCount' undeclared here (not in a function)这位新手非常感谢你的帮助。
发布于 2021-09-22 10:37:05
在C中,当结构被声明时,你不能引用结构中的其他字段。这就是错误消息所指的内容。如前所述,您可以使用指针或灵活的数组成员(fam):
lv_obj_t **radiobtnVIEW;lv_obj_t *radiobtnVIEW[];对于fam,您可以为所述字段动态分配内存:
size_t n = 4;
AssetVIEW *View = malloc(sizeof(*View) + n * sizeof(*View->radiobtnVIEW));
...
free(View);请注意,widgetCount上的const限定符没有意义,因为您无法对其进行初始化。
https://stackoverflow.com/questions/69279173
复制相似问题