【问题标题】:request for member in something not in structure请求非结构中的成员
【发布时间】:2015-06-24 05:25:37
【问题描述】:

嗨,我有以下功能,它给我的错误是

错误:在非结构或联合的情况下请求成员“prev”

void insert(struct node **start){
        struct node *temp=*start,*next_node,obj;
        int pos=0;
        if(temp==NULL)
        {
                printf("The linked list is empty creating the linked list !!!!!!\n");
                next_node=malloc(sizeof(struct node));
                if(next_node==NULL)
                {
                        printf("\n Sorry out of Memory !!!!!!! ");
                }
                else
                {
                        printf("\n Adding the first element in the list !!!!!!");
                        enterData(&obj);
                        next_node->data1=obj.data1;
                        next_node->data2=obj.data2;
                        next_node->prev=NULL;
                        next_node->next=NULL;
                        *start=next_node;
                        printf("\n The linked list created sucessfully !!!!!!!!! ");
                }
        }
        else{
                printf("Enter the position where you want to Enter the element \n");
                scanf("%d",&pos);
                if(pos==1)
                {
                        printf("\n Adding the element at the begining of the list !!!!! ");
                        next_node=malloc(sizeof(struct node));
                        enterData(&obj);
                        next_node->data1=obj.data1;
                        next_node->data2=obj.data2;
                        next_node->prev=NULL;
                        next_node->next=*start;
                        *start->prev=next_node; //The error is at this line of code
                        *start=next_node;
                }
        }
}

但是如果不是在上面提到的代码行中使用*start 以及注释如果我使用temp 这也是指向结构的指针。然后代码工作正常..?为什么会这样?

【问题讨论】:

  • *start 更改为(*start)-> 可能优先于 *
  • 更详细的评论(因为自上一条评论以来已经给出了答案):start的类型是struct node **temp 的类型是 struct node *。因此,您的问题“为什么*start 会产生错误而temp 不会”是有充分理由的。在这一点上,你甚至不需要知道任何关于操作符优先规则的事情。只需尝试将*start 替换为(*start),看看是否能解决问题...

标签: c pointers struct


【解决方案1】:

*start->prev 替换为(*start)->prev,因为-> 运算符比C precedance table 中的* 运算符绑定更紧密。

当你写信时:*start->prev
您希望它是:(*start)->prev
但实际上是:*(start->prev)


附言。正如人们所期望的那样,运算符的顺序不是从左到右(英语的阅读方向)。例如2 + 3 * 5 的结果是17not 25。这是因为* 将在执行+ 之前执行,因为* 运算符的优先级高于+ 运算符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多