c - How do I traverse a memory block in a struct with pointers? -
goal: traverse vertices
memory block in heap
struct.
errors: expected unqualified-id before ‘(’ token
when trying access vertices
. also, ‘vertices’ not declared in scope
.
header:
typedef struct heap *priorityqueue_t; typedef struct heapitem *priorityitem_t; priorityqueue_t init(int max_capacity, int source_vertex);
implementation:
#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; < q->size; i++) { q->(vertices + i)->id = i; //errors on line. q->(vertices + i)->distance = int_max; } q->(vertices + source_vertex)->distance = 0; return q; }
you've placed parens in wrong place. try this:
(q->vertices + i)->id = i; //errors on line. (q->vertices + i)->distance = int_max; } (q->vertices + source_vertex)->distance = 0;
Comments
Post a Comment