简体   繁体   English

为什么我们使用点/箭头运算符来访问 c 中的结构变量

[英]why we are using dot/arrow operator to access structure variables in c

I dont kno why we are using this to access variables.我不知道为什么我们使用它来访问变量。 Is there any standards?有什么标准吗? if there what are they?如果有它们是什么?

#include <stdio.h>
struct st {
  int i;
  char ch;
} s;
int main() {
  s.i = 10;
  printf("%d\n", s.i);
}

The dot operator and the arrow operator are not the same:点运算符和箭头运算符不一样:

The dot operator takes the attribute of a structure.点运算符采用结构的属性。
The arrow operator takes the attribute of the structure, the pointer you are using refers to.箭头运算符采用结构的属性,您使用的指针是指。

These two lines are the same thing:
(*(*(*a).b).c).d

a->b->c->d

It just seems more practical and better to look at, otherwise you'd have to use the one at the top which seems very hard to read, so we use the -> operator because it's much simpler.它看起来更实用,更好看,否则你必须使用顶部的那个看起来很难阅读,所以我们使用 -> 运算符,因为它更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用箭头运算符计算结构的 3 个成员的平均值,需要将点运算符转换为箭头 - using arrow operator to calculate average of 3 members of a structure, need to convert from dot operator to arrow 为什么我们在结构数组中使用点运算符 (.) 而不是箭头运算符 (-&gt;) 作为函数参数? - Why do we use the dot operator (.) instead of the arrow operator (->) in arrays of structs as function parameters? 具有简单结构的C“-&gt;”(箭头运算符)段故障 - C “->” (Arrow Operator) Seg Fault with Simply Structure C(指针和结构)错误中的箭头运算符 - Arrow operator in C (pointer and structure) error 在结构体中使用点运算符和箭头运算符以及字符串指针 - Using dot operator vs arrow operator with string pointer in structs 访问结构成员而不使用点运算符 - Accessing structure members without using dot operator C:点/箭头操作员如何在引擎盖下工作? - C: how does dot/arrow operator work under the hood? 为什么某些C程序使用箭头运算符指向结构的某些部分而不是直接引用? - Why do certain C programs use an arrow operator to point to parts of a structure instead of directly referencing? 使用箭头 -&gt; 和点。 运算符一起在 C - Using arrow -> and dot . operators together in C 为什么 C 中的箭头 (-&gt;) 运算符存在? - Why does the arrow (->) operator in C exist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM