This question already has an answer here:
What does this mean “->”?
                                
                                    (1个答案)
                                
                        
                                3年前关闭。
            
                    
我必须明天早上10点才能学到决赛,因为我是拖延症患者。

如果您愿意,我知道所有人都可以责骂我。我要说的是,我必须了解指针才能理解链表,而使用指针时使用->会使我困惑。

根据我搜索过的内容,它会更改变量指向的内容吗?

当教授给我的示例代码中有行时(例如此代码中的temp = temp->head或当其声明printf("%d", temp->num);时),这使我感到困惑

谁能帮忙解释一下吗?先感谢您。

    #include <malloc.h>
    #include <stdio.h>
    #include <stdlib.h>

    struct node
    {
        int num;
        struct node *ptr;
    };

     typedef struct node NODE;

     int main()
     {
      NODE *head, *temp = 0, *first;
    int count = 0;
    int choice = 1;

    while(choice)
    {
        head = (NODE *)malloc(sizeof(NODE));

    printf("Enter the value: \n");
    scanf("%d", &head->num);

    if (first != 0)
    {
        temp->ptr = head;
        temp = head;
    }
    else
        first = temp = head;
    fflush(stdin);
    printf("Do you want to continue?");
    scanf("%d", &choice);
    }

    temp->ptr = 0;
    temp = first;
    printf("\nStatus of the linked list");

    while (temp != 0)
    {
        printf("%d->" temp->num);
    count++;
    temp = temp->ptr;
    }
    printf("NULL\n");
    printf("Number of entries in linked list %d", count);
    return 0;
    }
}

最佳答案

它取消引用结构的指针,并访问该结构的成员。

以您的代码为例:

temp->ptr = head;


这和

(*temp).ptr = head;


“箭头”运算符使其更简单。

如果您有好奇心,请输入some history behind the -> operator

关于c - ->运算符在C中做什么? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41055412/

10-11 15:23
查看更多