typedef struct{
int number;
char name[100];
} Apple
typedef struct{
Apple *apple;
int bit[2];
} AppleArray;
int main(){
AppleArray *aArray;
loadApple(&aArray);
}
loadApple(AppleArray **aArray){
*aArray = NULL;
for(i=0; i<100; i++){
*aArray = realloc(*aArray, (i+1) * sizeof(AppleArray));
/*SEGFAULT here*/
aArray[i]->apple = NULL;
for(j=0; j<2; j++){
aArray[i]->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1))
}
}
}
我想擁有一個具有特定大小的AppleArray。每個AppleArray將有兩個Apple。但是,當我將NULL分配給aArray [i] - > apple時,我得到段錯誤。那裏有什麼問題?獲取結構數組中的結構的值
編輯:
loadApple(AppleArray **aArray){
*aArray = malloc(100 * sizeof(AppleArray));
for(i=0; i<100; i++){
/*SEGFAULT here*/
aArray[i]->apple = NULL;
for(j=0; j<2; j++){
aArray[i]->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1))
}
}
}
我認爲它應該是* aArray [i]。請檢查我的答案。 – Jay
@Jay錯誤:行中的賦值中的不兼容類型* aArray [0] - > apple = NULL; –
Try(* aArray)[0] .apple = NULL; – Jay