Error: not a member of structure or union, causing memory leaks

Error: not a member of structure or union, causing memory leaks

我试图在 c 中创建一个链表,它可以将字符串作为数据,并且我已经实现了推送、释放和弹出函数,但我的问题是在弹出函数中,它无法将名称识别为成员当我尝试释放它时。

typedef struct list
{
    char* name;
    struct list* next;
} node;


void free_list(node*head)
{
    if(head==NULL){
        return;
    }
    node* temp=NULL;
    while (head!= NULL)
    {
        temp=head->next;
        free(head->name);
        free(head);
        head=temp;
    }
    head=NULL;
}

/*add elements to the front of the list*/
void push_list(node **head, char* name)
{
    node *temp=malloc(sizeof(node));
    if(temp==NULL)
    {
        fprintf(stderr,"Memory allocation failed!");
        exit(EXIT_FAILURE);
    }
    temp->name=strdup(name);
    temp->next=*head;
    *head=temp;
}


void pop_list(node ** head) {
    node * next_node = NULL;
    if (*head == NULL) {
        return;
    }
    next_node = (*head)->next;
    free(*head->name); //this line generating error
    free(*head);
    *head = next_node;
}

bool empty_list(node *head){
    return head==NULL;
}

我猜这与我使用指向指针的指针错误有关?有点卡住了

您需要在 (*head) 两边加上圆括号以构成语句 free((*head)->name);free(*head->name) 被解释为 free(*(head->name)),这就是编译器对你大喊大叫的原因。

引用this Stack Overflow post,原因是因为post固定运算符(->)比一元运算符(*)具有更高的优先级。