【问题标题】:Why is the '->' operator not working in this case of a pointer to a pointer?为什么'->'运算符在这种指向指针的情况下不起作用?
【发布时间】:2023-03-17 12:35:01
【问题描述】:

我尝试使用链表实现队列数据结构。 在函数 Enqueue 中,我需要返回前后指针的值,因为函数不能返回两个值,所以我使用了指向指针的指针:前指针和后指针。 运算符“->”似乎不起作用,我收到以下错误代码:

main.c:67:18: error: ‘*rear’ is a pointer; did you mean to use ‘->’?
             *rear->next=temp;
                  ^~
                  ->
main.c: In function ‘dequeue’:
main.c:78:14: error: ‘*front’ is a pointer; did you mean to use ‘->’?
      x=*front->data;
              ^~
              ->
main.c:80:19: error: ‘*front’ is a pointer; did you mean to use ‘->’?
      *front=*front->next;//correctly moving front pointer
                   ^~
                   ->

任何帮助将不胜感激。

//implementing queue
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
    int data;
    struct node* next;
} *node;

//declaring functions
void createqueue();
void enqueue(node *front,node * rear, int x);
void dequeue(node *front, node * rear);
void display(node front);
void main(){
    node f=NULL;//pointer to a node
    node r=NULL;//pointer to a node
    
    char cont;
    cont='y';
    do{
        int x;
        int ch;
        printf("\n\nMain Menu:\n\n1.Create a Queue\n2.Enqueue an element onto the queue\n"
        "3.Dequeue an element of the queue\n4.Display the queue\n5.Quit\n");
        printf("\nEnter your choice: ");
        scanf("%d",&ch);
        switch(ch){
            case(1):
            createqueue();
            break;
            
            case(2):
            printf("Enter element : ");
            scanf("%d",&x);
            enqueue(&f,&r,x);
            break;
            
            case(3)://
            dequeue(&f, &r);
            break;
            
            case(4):
            display(f);
            break;
            
            case(5)://Quit
            exit(0);
        }
        printf("\nEnter 'y' or 'Y' to continue : ");
        scanf(" %c",&cont);
    }while(cont=='y'||cont=='Y');
}
void createqueue(){
    printf("Queue created!\n");
}
//the return values cannot be 2 in no. so we will have a pointer to the rear and front variables which are themselves pointers
void enqueue(node *front,node *rear,int x){//and not Enqueue(Node * front,Node * rear, int x){
    node temp=malloc(sizeof(struct node));
    if(temp!=NULL){
        temp->data=x;
        temp->next=NULL;
        //if both front and rear are null then both will change//temp and rear=null case:
        if (*front==NULL && *rear==NULL)
        *front=*rear=temp;//if there was only one element, the above case will happen for adding that first element and from the second element on, the else conditional would hold true
        //not empty queue case
        else{
            *rear->next=temp;
            *rear=temp;
        }
    }
    else
    printf("Unable to allocate memory for the node\n");
}
void dequeue(node *front,node *rear){
    int x;
    node t;
    if(*front!=NULL){//list not empty
        x=*front->data;
        t=*front;//t stores the address of the first node for freeing up later
        *front=*front->next;//correctly moving front pointer
        free(t);//free(address) is the syntax. free(**front) is not same as free(address of node) rather it is free(node) itself.
        if(*front==NULL)//if now after removing one element the list gets empty, then
        *rear=NULL;//otherwise rear will still be pointing to the address just freed
        printf("%d dequeued\n",x);
    }
    else
    printf("The list is empty!");
    
}
void display(node front){
    node t=front;
    int count=1;
    if(front!=NULL){
        while(t!=NULL){
            printf("%d. %d\n",count++,t->data);
            t=t->next;
        }
    }
    else
    printf("List is empty!");
}

【问题讨论】:

  • typedef struct node {...} *node; 不要那样做,或者如果你这样做了,当你爱上它时不要感到惊讶。
  • 您需要查看:Is it a good idea to typedef pointers?。 (一般 -- 否)

标签: c pointers


【解决方案1】:

也许你的函数声明应该更像

void 函数(节点*前,节点*后);

有时修复语法中的小错误可以消除许多其他错误。

【讨论】:

    【解决方案2】:

    这是初学者犯的一个非常微妙的错误。 *rear->next=temp 用法错误 (*后)->下一个=温度;是正确的用法。 其他任务也类似。 这遵循运算符的优先级。

    【讨论】:

      【解决方案3】:

      这是运算符的优先级问题:

      *rear-&gt;next=temp 表示*(rear-&gt;next)=temp

      但你需要的是:

      (*rear)-&gt;next=temp

      所以你必须明确地使用括号。

      【讨论】:

        【解决方案4】:

        自从我使用 c++ 以来已经过去了二十年,但如果我看到你的错误我会尝试

        *front = front-&gt;next

        因为看起来您想将传入指针的值更改为列表中的下一项。

        【讨论】:

        • 嘿!谢谢,但这不起作用,因为我需要将存储在前面的地址(这里是 *front)更改为存储在前面的下一个地址(即 *front->next。
        猜你喜欢
        • 1970-01-01
        • 2013-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        • 2017-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多