(c) struct member references (arrows) and. (points)

Source: Internet
Author: User

References to struct members have such a pattern:

Arrow (): The left must be a pointer;
Point number (.): The left must be an Entity.

So what if a struct pointer refers to a member and the member is a struct (and an entity), what if you want to refer to the members of this member?

After the experiment found, still follow the above rule namely: the arrow left must be the pointer, the entity must use the dot number reference. For example C->student.age

eg.

You start with an arrow that's C->s1->age.

#include"stdio.h"intmain () {structstudent{intage ; intclass_;    }; structclass2{structStudent s1;    }; structClass2 *c; structClass2 cc={. S1={. age=9,. Class_=2    }    }; C=&cc; printf ("%d",c->s1->age ); return 0;}

An error occurs as Follows:

If you change the code to c->s1.age, the code is as Follows:

#include"stdio.h"intmain () {structstudent{intage ; intclass_;    }; structclass2{structStudent s1;    }; structClass2 *c; structClass2 cc={. S1={. age=9,. Class_=2    }    }; C=&cc; printf ("%d",c->s1.age); return 0;}

Compile by getting the result of the Run:

(c) struct member references (arrows) and. (points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.