lucky88shp
lucky88shp

Reputation: 61

Trying to dereference/use double pointer in C

So I am having trouble using/dereferencing a double pointer in C. It gives me the error message request for member * in something not a structure or union. Now, I saw many post with similar questions but, the solutions like doing (*head) and head = &temp do not work. Can someone just help me, please?

vertex_t **create_graph(int argc, char *argv[]) {
   vertex_t **head, *temp;

   temp = malloc(sizeof(vertex_t));

   head = head->temp;
   head->name = argv[1];

   head->next = malloc(sizeof(vertex_t));
   head->next->name = argv[2];
   head->next->next = 0;

   head->adj_list = malloc(sizeof(adj_vertex_t));
   head->adj_list->edge_weight = atoi(argv[3]);
   head->adj_list->vertex = head->next;

   head->next->adj_list = malloc(sizeof(adj_vertex_t));
   head->next->adj_list->edge_weight = atoi(argv[3]);
   head->adj_list->vertex = head;

   return head;
}

Upvotes: 1

Views: 781

Answers (2)

Charan Pai
Charan Pai

Reputation: 2318

is this meets your requirements ?

vertex_t **create_graph(int argc, char *argv[]) {
vertex_t *head, **temp;

head = malloc(sizeof(vertex_t));
temp = malloc(sizeof(vertex_t*));
*temp = head;

head->name = argv[1];

head->next = malloc(sizeof(vertex_t));
head->next->name = argv[2];
head->next->next = 0;

head->adj_list = malloc(sizeof(adj_vertex_t));
head->adj_list->edge_weight = atoi(argv[3]);
head->adj_list->vertex = head->next;

head->next->adj_list = malloc(sizeof(adj_vertex_t));
head->next->adj_list->edge_weight = atoi(argv[3]);
head->adj_list->vertex = head;

 return temp;
}

Upvotes: -1

nneonneo
nneonneo

Reputation: 179717

Everything there says you should be using vertex_t *head.

Also, head = head->temp; is going to crash since you haven't yet assigned head.

Upvotes: 3

Related Questions