Simple learning C-day 5 and day 5

Source: Internet
Author: User

Simple learning C-day 5 and day 5

Struct

First, it is clear that struct is a type of constructed data, which is composed of multiple data types, such as int, char, double, array, or struct ...... the type of the struct. When defining int integer variables, everyone must knowInt;That is to say, if an int type variable is defined, the system will automatically allocate space such as int, but for struct, it is composed of many uncertain types, therefore, when defining it, you need to specify the data types it consists. there are multiple definition formats. Here, only one is simple. The definition format is as follows.

Struct name {data types}; // note that the semicolon at the end must contain

 

For example, if I want to define a student, what attributes do students need to pay attention? Assume that you need to count the student's student ID 1, 2 Name, 3 Gender, 4 class, 5 phone number, and 6 Final score. In this case, you need to use a structure for simplicity and convenience.

Struct student // struct name {long number, tel; // student ID and phone int score; // char name [20], sex [4], clas [20]; // name, gender, and class };

As shown above, a structure about student information is successfully defined. The following describes the specific input and output operations.

# Include <stdio. h> struct student {long number, tel; // student ID and phone int score; // char name [20], sex [4], clas [20]; // name, gender, and class}; int main () {struct student tiantian; // declares a variable named tiantian, the declaration can be performed simultaneously with the definition. // you can enter scanf ("% lld % s % lld % d", & tiantian. number, tiantian. name, tiantian. sex, tiantian. clas, & tiantian. tel, & tiantian. score); // output printf ("% lld % s % lld % d", tiantian. number, tiantian. name, tiantian. sex, tiantian. clas, tiantian. tel, tiantian. score); return 0 ;}

Running result:

 

Maybe after reading the above, you will think, this is too troublesome. I will define 6 independent variables and input them and output them, the results are the same, and the code is much more concise. You do not need to add the variables declared when the struct is declared before each variable. This is true for the above, but I would like to say, please count all the people in the class their respective student ID 1, 2 names, 3 Gender, 4 class, 5 phone calls, 6. At the end of the final score, you can see the role of the struct. For details, refer to the following code.

 

# Include <stdio. h> struct student // struct name, defined as {long number, tel; // student ID and int score; // char name [20], sex [4], clas [20]; // name, gender, and class}; int main () {// assume that the number of students in this class is lower than 100 and an array variable named stu is declared, // each element in the array can store the six information of the next student, struct student stu [100]; int I; // input, in a loop, j assume that there are continuing students for (I = 0; I <5; I ++) scanf ("% lld % s % lld % d ", & stu [I]. number, stu [I]. name, stu [I]. sex, stu [I]. clas, & stu [I]. tel, & stu [I]. score); // output for (I = 0; I <5; I ++) printf ("% lld % s % lld % d \ n ", stu [I]. number, stu [I]. name, stu [I]. sex, stu [I]. clas, stu [I]. tel, stu [I]. score); return 0 ;}

Running result:

 

Observe the above results, input five groups of data, press enter, and output the results stored in the array, each student row, each student's information is blank. When the number is small, you may not be able to see the advantage. However, if the number of student data records Reaches thousands, it is a good solution to use a structure to record student data.

 

For example, I want to add another information in the above struct. What should I do at this time?

You can use the nesting of struct. In C, many things can be nested. Modify the definition of the above struct:

# Include <stdio. h> struct date // indicates the struct {int year, month, day ;}; struct student // struct name, which defines {long number, tel; // student ID and phone number int score; // score char name [20], sex [4], clas [20]; // name, gender and class struct date bri_day; // set the above expression date struct to the student structure to express the student's birthdate}; int main () {struct student stu [100]; // statement // content}

One problem can be found from the above changes. What should I do when I access the website? Of course, it is also layer-by-layer access. For example, the access to the student's birth year should be:Stu [I]. bri_day.year;In addition, we can see that in the main function, main ()

It only declares the student struct and does not declare the date struct. Do you not need to declare the date struct? Certainly not. Note: Within the student struct, I have declared the date struct and declared the variableBri_day.

Of course,Similar to struct types, there are also common union and enumeration enum types, which are similar in usage. After understanding the struct, let's see that they are small cases.

Related Article

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.