[C language-7] struct struct

Source: Internet
Author: User

A. Basic knowledge

An array of contrasts with arrays: constructed types can only have more than one data of the same type: struct types may consist of several different types of data consisting of 1. Defining types
1     struct Student2     {3         int age;4         char *name;5         float height;6     };
2. Define a struct variable define a variable in several ways a.
1     //define a struct variable2     struct Student stu = {, "Simon", 1.65f};
B.
1     struct Student {2         int age;3         char *name;4         float height;5     } stu = {, "Simon", 1.65f};
1/     * ERROR notation 2     struct Student p;3     p = {, "Tom"};4      */
C.
1     struct Student p;2     p.age = 17;3     p.name = "Tom";
D.
1     struct Student p2 = {, "Sam"};

E.
1     struct Student p3 = {. Name= "Judy",. Age= 44};
F. Anonymous
1     struct2     {3         int age;4         char *name;5     } stu3;
3. The struct is not allowed to define a recursive definition of the variable 4 within the struct construction code that defines the structure. can contain other structures
1 void Test1 () 2 {3     struct Date 4     {5         int year, 6         int month; 7         int day; 8     }; 9     St Ruct Student11     {         25 int age;13         struct Date birthday;14     };15     struct Student stu 1989, 8, 10}};17     printf ("My Birthday ==>%d-%d-%d\n", Stu.birthday.year, Stu.birthday.month, Stu.birthday.day); 19}
5. Structure array
1     struct Student2     {3         int age;4         char *name;5         float height;6     } stus[5];
Anonymous
1     struct2     {3         int age;4         char *name;5         float height;6     } stus[5];
6. As a function parameter, the struct is passed only the value of the member 7. Pointers to struct bodies
1 void Test4 () 2 {3     struct person p = {$, ' ss '}; 4     struct person *pointer; 5     6     pointer = &p; 7
   
    8     printf ("Test4:person ' s age =%d\n", p.age);//it's P!!!, not pointer!! 9     printf ("Test4:person's age =%d\n", (*pointer) (age)     , printf ("Test4:person's age =%d\n", pointer->age); 11}
   
==> 3 ways to access struct member variable A. Struct.vb. (*pointer). VC. Pointer->v 8. Structure in-vivo allocation a. Storage space is not allocated when defining a struct variable when allocating space, the allocated space size is a multiple of the maximum member variable 9. Structure replication
1     struct Student p2 = {, "Sam"};2     struct Student p3 = {. Name= "Judy",. age= 44};3     p3 = p2;4     printf ("p 3:age->%d, name-.%s\n ", P3.age, P3.name);
Out:p3:age->17, Name-> Sam

[C language-7] struct struct

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.