简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM