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

问题描述 投票:-3回答:2

我不知道为什么我们使用它来访问变量。有什么标准吗?如果有的话?

#include <stdio.h>
struct st {
  int i;
  char ch;
} s;
int main() {
  s.i = 10;
  printf("%d\n", s.i);
}
c structure
2个回答
1
投票

点运算符和箭头运算符不相同:

点运算符采用结构的属性。箭头运算符采用结构的属性,您正在使用的指针指向该结构。


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

a->b->c->d

看起来似乎更实用,而且更好看,否则,您必须使用顶部的似乎很难阅读的那个,所以我们使用->运算符,因为它更简单。

© www.soinside.com 2019 - 2024. All rights reserved.