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

Posted by pyroxene on Stack Overflow See other posts from Stack Overflow or by pyroxene
Published on 2012-06-12T10:38:32Z Indexed on 2012/06/12 10:39 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

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..?

© Stack Overflow or respective owner

Related posts about c

    Related posts about pointers