(C) Structure member reference-> (arrow) and. (point), member arrow
The reference of struct members has the following rule:
Arrow (->): the pointer must be left;
Point (.): The object must be on the left.
So if a struct pointer references a member and the member is a struct (and an entity), what should we do if we want to reference the member?
After the experiment, we found that the above rule is still followed: the pointer must be on the left of the arrow, and the object must be referenced with a point number. For example C-> student. age
Eg.
Start with the arrow c-> s1-> age
#include "stdio.h"int main(){ struct student{ int age; int class_; }; struct class2{ struct student s1; }; struct class2 *c; struct class2 cc={ .s1={ .age=9, .class_=2 } }; c=&cc; printf("%d",c->s1->age); return 0;}
The following error occurs:
If you change the code to c-> s1.age, the Code is as follows:
#include "stdio.h"int main(){ struct student{ int age; int class_; }; struct class2{ struct student s1; }; struct class2 *c; struct class2 cc={ .s1={ .age=9, .class_=2 } }; c=&cc; printf("%d",c->s1.age); return 0;}
The compilation result is obtained: