为什么我会收到“要求不是结构体或联合体的成员的请求"?从此代码? [英] Why do I get "request for member in something not a struct or union" from this code?

查看:207
本文介绍了为什么我会收到“要求不是结构体或联合体的成员的请求"?从此代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过编写一个链表来自学C.我是指针和内存管理的新手,我有点困惑.我有以下代码:

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

从列表中删除一个元素.如果要删除第一个节点,我仍然需要某种方式引用列表,这就是为什么listP arg是双指针的原因,因此它可以指向列表的第一个元素,并允许我释放列表中的第一个元素.曾经是头部的节点.但是,当我尝试取消引用listP以访问指向下一个节点的指针时,编译器告诉我error: request for member ‘next’ in something not a structure or union .我在这里做错了什么?我想我可能会无可救药地混在一起..?

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

推荐答案

此:

*listP->next

与此相同:

*(listP->next)

您想要这个:

(*listP)->next

这篇关于为什么我会收到“要求不是结构体或联合体的成员的请求"?从此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆