C-language structural body

Source: Internet
Author: User

The data in the actual problem often has different types of data types, such as students in school, age (int), height (float type), weight (float type), name (string type), gender (char), and so on, which are the attributes that each student should have. We must not be able to use the data types previously learned to express these data, the structure of this new data type is born, whether it is the C language or OC language has its place.

    • A struct is a construction data type, which means that we must define (construct) it before it is used, so the general form of the struct is:
1     struct struct Name {2         type 1 member name 13         type 2 member name 24        ...  ... 5     }6     

Where struct is the keyword, is the structure of the type of symbol, the structure of the name of the general First Capital letter

For example, define a student's structure: ( note the braces and the semicolon at the end )

1     struct student{2         int Age ; 3         float weight; 4         Char *name; 5     };
    • struct type definition, to use the structure to define the structure of the variable, some people are confused by these concepts, in fact, it is well understood that the definition of a struct is like a description of the type of int data has what characteristics, and the definition of struct variable is similar to the following statement:
1 int A;

(Use the structure above)

struct Student Stu;

Where Stu is the structure variable name, struct student indicates that the STU variable is the student type structure, the structure type definition and the structure body variable definition we can complete one step:

1     struct student{2         int Age ; 3         float weight; 4         Char *name; 5     } Stu

We can also define multiple structural body variables:

1     struct student{2         int Age ; 3         float weight; 4         Char *name; 5     } STU1, STU2, STU3;

The STU1, STU2, and STU3 are all structural variables of the student type.

    • Initialization of struct type variables
1     //Error code2     structstudent{3         intAge ;4         floatweight;5         Char*name;6 }stu1;7     8STU1 = { -,58.4,"Zhangsan"};

This is an easy mistake for beginners, so the overall initialization of a struct variable can only be one of the following four ways:

1     //The first way2     structstudent{3         intAge ;4         floatweight;5         Char*name;6}stu1= { -,58.4,"Zhangsan"};7     8     //The second way9     structstudent{Ten         intAge ; One         floatweight; A         Char*name; -     }; -      the     structStudent STU1 = { -,58.4,"Zhangsan"}; -      -     //The Third Way -     structstudent{ +         intAge ; -         floatweight; +         Char*name; A}STU1 = {. Weight =58.4,. Age = -,. Name ="Zhangsan"}; at      -     //Fourth Way -     structstudent{ -         intAge ; -         floatweight; -         Char*name; in     }; -      to     structStudent stu1 = {. Weight =58.4,. Age = -,. Name ="Zhangsan"};

The first and second ways to pay attention to the order of each data, to correspond together, as for the third and fourth way to access the structure member variable point ('. ') ) method, the assignment of these two methods to idiom variables can be

    • The general form of the Narimoto variable that accesses the struct is: struct variable name . member Variable name (middle connection is dot)
1#include <stdio.h>2 3 intMain () {4     5     //define a struct type6     structstudent{7         intAge ;8         floatweight;9         Char*name;Ten     }; One     //define a struct variable and initialize it A     structStudent STU1 = { -,58.4,"Zhangsan"}; -     //Modifying the value of a member variable -Stu1.age = -; the     //Output Modified value -printf"%d\n", stu1.age); -     return 0; -}
    • Definition and initialization of struct arrays
1#include <stdio.h>2 3 intMain () {4     5     //define a struct type6     structstudent{7         intAge ;8         floatweight;9         Char*name;Ten     }; One     //defines an array of structs and initializes A     structStudent stu[3] = {{ -,58.4,"Zhangsan"}, -{ the,54.3,"LiSi"}, -{ -,55.0,"liming"}}; the     //output member variable values for age in each struct array -printf"%d---%d---%d\n", stu[0].age,stu[1].age,stu[2].age); -     return 0; -}

    • struct pointers, because struct variables have their own storage addresses in memory, so you can definitely manipulate them with pointers.

definition of struct variable pointer :

1     struct student{2         int Age ; 3         float weight; 4         Char *name; 5     } *p_stu;

It could be that way.

1     struct student{2         int Age ; 3         float weight; 4         Char *name; 5     }; 6     7     struct Student *p_stu;

So how does the pointer access the member variable? There are two ways of

    1. (* pointer variable name). Member Name
    2. Pointer variable name, member name

Code implementation:

1#include <stdio.h>2 3 intMain () {4     5     //define a struct type this time memory has not yet allocated space to the struct6     structstudent{7         intAge ;8         floatweight;9         Char*name;Ten     }; One     //defines a struct pointer, at which time the system assigns a space of the corresponding size to the pointer variable A     structStudent *P_stu; -      -P_stu->age =Ten; theP_stu->name ="Zhangsan"; -  -printf"%d---%s\n", P_stu->age, (*p_stu). Name); -     return 0; +}

    • Attention to the use of structural bodies

C-language structural body

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.