user3303509
user3303509

Reputation: 13

How to access a variable in an array of struct's

In the for loop below I attempt to access an array of structures stored in another structure but I continue to get an error that says. "struct list has no member head."

list* createList(int size)
{
    list* graph = malloc(sizeof(list));
    graph->size = size;
    graph->array = malloc(size * sizeof(vertex));
    int i;
    for(i=0; i < size;i++){
        graph->array[i].head = NULL;
        return graph;
    }
}

The structures I am attempting to use are as follows.

struct vertex
{
    struct vertex *head;
};
typedef struct vertex vertex;

And

struct list
{
    int size;
    struct list* array;
};
typedef struct list list;

Upvotes: 0

Views: 74

Answers (2)

igoris
igoris

Reputation: 1476

You don't have any references to your struct vertex in struct list. I suspect that it should be

struct list{
     int size;
     struct vertex* array;
};
typedef struct list list;

Upvotes: 1

OlivierLi
OlivierLi

Reputation: 2846

Your list member is of type list, when it should be of type vertex.

Upvotes: 2

Related Questions