目标:
遍历structvertices
中的内存块。Heap
错误:我expected unqualified-id before ‘(’ token
在尝试访问vertices
. 另外,‘vertices’ was not declared in this scope
.
标题:
typedef struct Heap *PriorityQueue_t;
typedef struct HeapItem *PriorityItem_t;
PriorityQueue_t Init(int max_capacity, int source_vertex);
执行:
#include "heap.h"
#include <stdlib.h>
#include <limits.h>
struct Heap
{
int size;
PriorityItem_t vertices;
};
struct HeapItem
{
int id;
int distance;
};
PriorityQueue_t Init(int max_capacity, int source_vertex)
{
PriorityQueue_t Q;
Q = (PriorityQueue_t)malloc(sizeof(PriorityQueue_t));
Q->vertices = (PriorityItem_t)malloc(sizeof(PriorityItem_t)*max_capacity);
Q->size = max_capacity;
int i;
for(i = 0; i < Q->size; i++)
{
Q->(vertices + i)->id = i; //Errors on this line.
Q->(vertices + i)->distance = INT_MAX;
}
Q->(vertices + source_vertex)->distance = 0;
return Q;
}