Why do I get “request for member in something not a struct or union” from this code?

寵の児 提交于 2019-12-13 04:18:30

问题


I'm trying to teach myself C by coding up a linked list. I'm new to pointers and memory management and I'm getting a bit confused. I have this code:

/* Remove a node from the list and rejiggle the pointers */
void rm_node(struct node **listP, int index) {
    struct node *prev;
    struct node *n = *listP;
    if (index == 0) {
        *listP = *listP->next;
        free(n);
        return;
    }
    for (index; index > 0; index--) {
        n = n->next;
        if (index == 2) {
        prev = n;
        }
    }
    prev->next = n->next;
    free(n);
}

to remove an element from the list. If I want to remove the first node, I still need some way of referring to the list, which is why the listP arg is a double pointer, so it can point to the first element of the list and allow me to free the node that used to be the head. However, when I try to dereference listP to access the pointer to the next node, the compiler tells me error: request for member ‘next’ in something not a structure or union . What am I doing wrong here? I think I might be hopelessly mixed up..?


回答1:


This:

*listP->next

is the same as this:

*(listP->next)

You want this instead:

(*listP)->next


来源:https://stackoverflow.com/questions/10994957/why-do-i-get-request-for-member-in-something-not-a-struct-or-union-from-this-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!