首页 > 解决方案 > “pointer->register_next->value”是否与 C 中的“pointer->register_next.value”相同?

问题描述

我正在研究列表(C 语言)中的搜索过程,并且我已经看到过程在编写条件时同时使用箭头运算符点运算符。

对于像...这样的结构

struct node{
  int value; //value, can be any type
  struct node *next;
};

我见过...

if(current->next->value == searched_value)

...和

if(current->next.value == searched_value)

...正在使用。我的问题是:这些方法在任何给定情况下是否可以互换?(即它们是相同的)

标签: cpointerssearch

解决方案


不,它们绝对不一样,而且不可互换。

箭头运算符->仅在您有一个指向结构的指针作为变量时才有效。

所以:

struct *p;
p->a = 0; // correct
p.a = 0; //syntax error

显然,您一定一直在寻找其他东西,因为 ifnextstruct node *类型(指向 的指针struct node)然后,current->next.value 是一个错误。

gcc 你应该得到一个错误说:error: request for member ‘value’ in something not a structure or union


推荐阅读