问题
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