1

所以我在 C 中使用/取消引用双指针时遇到问题。它给了我对成员*的错误消息请求,而不是结构或联合。现在,我看到很多类似问题的帖子,但是,解决方案喜欢做(*head)head = &temp不工作。有人可以帮我吗?

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;
}
4

2 回答 2

3

那里的一切都说你应该使用vertex_t *head.

此外,head = head->temp;由于您尚未分配head.

于 2012-09-25T06:09:56.660 回答
-1

这符合您的要求吗?

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;
}
于 2012-09-25T07:21:45.590 回答