我在以下两行标记的行中收到错误消息“'->' 的类型参数无效” 请建议如何更正它
#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[0]
是一个结构,而不是指向结构的指针。所以你应该使用 .
运算符来访问成员:
H->array[0].distance = 20;
i = H->array[0].distance;
或组合它们:
i = H->array[0].distance = 20;
顺便说一句,在 C 中,您应该不强制转换 malloc()
的结果。 malloc()
返回 void*
,C 自动将其强制转换为目标类型。如果您忘记 #include
malloc()
的声明,转换将抑制您应该收到的警告。这在 C++ 中并非如此,但在 C++ 中,您通常应该更喜欢 new
而不是 malloc()
。
关于c - ‘->’ 的无效类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18860123/