Sha*_*ngh 5 c pointers structure
我收到错误,因为" - >'"的无效类型参数在下面两个标记的行上请建议如何纠正它
#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);
}
Run Code Online (Sandbox Code Playgroud)
左参数->必须是指针.H->array[0]是一个结构,而不是指向结构的指针.所以你应该使用.运算符来访问一个成员:
H->array[0].distance = 20;
i = H->array[0].distance;
Run Code Online (Sandbox Code Playgroud)
或合并它们:
i = H->array[0].distance = 20;
Run Code Online (Sandbox Code Playgroud)
顺便说一句,在C你应该不投的结果malloc().malloc()返回void*,C自动将其强制转换为目标类型.如果您忘记#include了声明malloc(),演员将取消您应该得到的警告.在C++中不是这样,但你通常应该更喜欢new而不是malloc()在C++中.
| 归档时间: |
|
| 查看次数: |
10624 次 |
| 最近记录: |