c - “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)
...正在使用。我的问题是:这些方法在任何给定情况下是否可以互换?(即它们是相同的)
解决方案
不,它们绝对不一样,而且不可互换。
箭头运算符->
仅在您有一个指向结构的指针作为变量时才有效。
所以:
struct *p;
p->a = 0; // correct
p.a = 0; //syntax error
显然,您一定一直在寻找其他东西,因为 ifnext
是struct node *
类型(指向 的指针struct node
)然后,current->next.value
是一个错误。
在gcc
你应该得到一个错误说:error: request for member ‘value’ in something not a structure or union
推荐阅读
- django - Django 视图仅适用于一个 url,尽管在视图中生成了适当的查询集
- python - 在 Python 中解压字典列表
- google-app-maker - onClick可以刷新页面吗?
- javascript - 限制为 2 位小数导致 JavaScript
- excel - dll缺少预期的功能
- symfony - 我无法添加新命名空间“,因为该名称已在使用中!”
- python - 如何从 html 获取用户输入并使用 Python 在数据库中搜索它
- flutter - 如何转换流
只返回 T? - javascript - 将缩小的 JavaScript 代码导入 HTML 文件而不粘贴
- sql - 我应该如何使用 SQL 构建我的井字游戏服务器