首页 > 解决方案 > 我正在使用'->',但输出仍在询问我是否打算使用'->'

问题描述

我正在尝试在 C 中创建单链表的功能,但在访问头节点的下一个节点时遇到问题。

typedef struct node {
  struct node *next;
} Node;

int foo(Node **head){
  *head = *head->next;
}

当我运行此代码时,我希望它将我的头节点指针的地址更改为下一个节点,但我收到以下错误:

‘*head’ is a pointer; did you mean to use ‘->’?
     *head = *head->next;

标签: cpointersdouble-pointer

解决方案


foo 内的行应该是

     *head = (*head)->next

因为 '->' 的优先级高于 *

您可以在此处了解有关运算符优先级的更多信息 ( https://en.cppreference.com/w/cpp/language/operator_precedence )


推荐阅读