【发布时间】:2013-05-30 01:05:15
【问题描述】:
我正在尝试创建一个名为 StatusItem 的结构数组,如下所示:
typedef struct
{
char* name;
char* index;
int optional;
} StatusItem;
此外,由于我希望此数组为任意大小,因此我使用的是malloc。所以数组是这样定义的:
StatusItem* statusItem = NULL;
(然后将其传递给检索所有值的函数,如下所示。)
statusItem = (StatusItem*)malloc(cJSON_GetArraySize(items));
...
for (i = 0 ; i < cJSON_GetArraySize(items) ; i++)
{
strcpy(statusItem[i].name,name->valuestring);
strcpy(statusItem[i].index,index->valuestring);
if(!parseInt(optional->valuestring, &statusItem[i].optional));
{
goto cleanup;
}
}
有代码涉及 cJSON 库将name、index 和optional 的字符串值获取到上面引用的变量中,并将它们存储在这些变量的valuestring 字段中。
我检查了涉及 cJSON 库的所有内容都可以正常工作,并返回正确的值,但程序无法访问或存储 statusItems 数组中的值。
有什么想法吗?我几乎可以肯定这涉及到我对malloc 的一些滥用。
【问题讨论】:
-
忘记了代码中的for循环,现在添加。
标签: c struct malloc strcpy cjson