"? (1 个回答) 已关闭 6 年前。 我必须在明天上午 10 点之前完成期末考试,因为我是个拖延者。 如果你愿意的话,我知道都-6ren">
gpt4 book ai didi

c - -> 运算符在 C 中起什么作用?

转载 作者:行者123 更新时间:2023-11-30 21:45:55 25 4
gpt4 key购买 nike

我必须在明天上午 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com