【发布时间】:2013-09-22 11:57:15
【问题描述】:
在下面两个标记的行上,我收到错误为“'->' 的无效类型参数” 请建议如何纠正它
#include<stdio.h>
struct arr{
int distance;
int vertex;
};
struct heap{
struct arr * array;
int count; //# of elements
int capacity;// size of heap
int heapType; // min heap or max heap
};
int main(){
int i;
struct heap * H=(struct heap *)malloc(sizeof(struct heap));
H->array=(struct arr *)malloc(10*sizeof(struct arr));
H->array[0]->distance=20;//error
i=H->array[0]->distance;//error
printf("%d",i);
}
【问题讨论】:
-
H->array包含 10 个struct arr,而不是 10 个struct arr *。所以不需要额外的取消引用。第一行应该是H->array[0].distance = 20; -
@Praetorian - 你为什么在评论中回答问题而不是作为实际答案?
-
@pzaenger 我最好说:不要转换 malloc 的结果。
-
@Andrew 当然是为了获得评论代表! :P 我认为简洁的评论不值得回答,也没有时间输入更详细的答案。