我正在尝试对结构customerInformation进行malloc。但是,我一直得到";错误:从类型"void*"分配给类型"struct CustomerInformation"时,类型不兼容;。我的申报单中遗漏了什么?如有任何帮助,我们将不胜感激。非常感谢。
struct CustomerInformation *result=malloc(sizeof(struct CustomerInformation)*100000);
for(int i=0;i<n;i++)
{
result[i]=malloc(sizeof(struct CustomerInformation));
}
result[i]
的类型为struct CustomerInformation
(不是指针(,但您正在分配一个void *
(指针(。
如果您想要指向结构数组的指针:
struct CustomerInformation *result = malloc(sizeof(struct CustomerInformation*) * 100000);
for(int i=0;i<n;i++)
{
result[i].cust_id = ...;
}
一个巨大的内存块,包含100000个CustomerInformation结构。
如果您想要指向指向结构的指针数组的指针:
struct CustomerInformation **result = malloc(sizeof(struct CustomerInformation*) * 100000);
for(int i=0;i<n;i++)
{
result[i] = malloc(sizeof(struct CustomerInformation));
result[i]->cust_id = ...;
}
一个巨大的内存块,包含100000个指针,再加上n
个较小的指针,每个指针包含一个CustomerInformation结构。