Simple study c--fifth day

Source: Internet
Author: User

Structural body

First, it is clear that the struct is a constructed data type, which is a kind of data type such as int,char,double, array or struct ... The type of composition that now tells you how to define a struct.   When defining an int integer variable, you must know int A; that is, an int variable is defined, and the system automatically allocates space as large as int, but for a struct it is made up of many indeterminate types, so when defining it, you need to indicate which data types it consists of. There are a number of definition formats, here is a simple one, The definition format is as follows.

struct name {   What data types are made up of}; // Note that the semicolon at the end must have

For example: I want to define a student, what are the attributes that students need to be aware of? Suppose you need to count the student's 1 school number, 2 name, 3 Gender, 4 class, 5 phone, 6 end result, at this time in order to be concise and easy to handle, you need to use the structure

struct // struct name {   longlong  //  School Number and phone   int//  Score   char name[],sex[4],clas[// name, Gender and Class };

As above, the successful definition of a student information structure, the following implementation of specific input and output operations

#include <stdio.h>structstudent{Long LongNumber,tel;//School Number and telephone    intScore;//Achievements   Charname[ -],sex[4],clas[ -];//name, Gender and class};intMain () {structStudent Tiantian;//declares a variable named Tiantian, which can be used in conjunction with the definition//You can now enterscanf"%lld%s%s%s%lld%d",&tiantian.number,tiantian.name,tiantian.sex,tiantian.clas,&tiantian.tel,&Tiantian.score); //Outputprintf"%lld%s%s%s %lld%d", Tiantian.number,tiantian.name,tiantian.sex,tiantian.clas,tiantian.tel,tiantian.score); return 0;}

Operation Result:

Perhaps after looking at the above, you will think, this is too much trouble, I casually define 6 independent variables, input and output it, so the effect is the same, and the code is much more concise, do not have to use each variable is added before the declaration of the structure of the variable declared. For the above this is right, but I would like to say, please count all the people in the class of their respective 1 school number, 2 name, 3 Gender, 4 classes, 5 telephone, 6 period results, then can appreciate the structure of the role of the large, specific look at the following code.

#include <stdio.h>structStudent//struct name, definition{    Long LongNumber,tel;//School Number and telephone    intScore;//Achievements   Charname[ -],sex[4],clas[ -];//name, Gender and class};intMain () {//assuming that the class has fewer than 100 students, declares an array variable named Stu,//each element in the array can hold six messages for the next student    structStudent stu[ -]; inti; //input,, with loops, J Suppose to have a renewed student     for(i=0;i<5; i++) scanf ("%lld%s%s%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%s%s %lld%d\n", Stu[i].number,stu[i].name,stu[i].sex,stu[i].clas,stu[i].tel,stu[i].score); return 0;}

Operation Result:

Observing the above results, input five sets of data, press ENTER, output the results stored in the array, each student row, each student's information between the empty one grid. It may not be possible to see the advantages when the numbers are small, but if the recorded student data reaches thousands of groups, it is no doubt that using the structure to record student data is a better solution.

For example, I want to add a message in the above structure, the student's date of birth, what should be done at this time?

Can use the structure of the nesting, in C language, a lot of things can be nested. Modifying the definition of the above structure can be done by:

#include <stdio.h>structDate//structure representing the date{    intYear,month,day;};structStudent//struct name, definition{    Long LongNumber,tel;//School Number and telephone    intScore;//Achievements    Charname[ -],sex[4],clas[ -];//name, Gender and class    structDate Bri_day;//set the structure of the expression date above to the student structure to express the student's birth date};intMain () {structStudent stu[ -];//Statement//content}

From the above changes can be found that there is a problem, visit the date of the day, how to do? Of course, is also a layer of access, such as access to the student's birth year, it should be:stu[i].bri_day.year; You can also see that I'm in the main function, which is main ()

It only declares the student struct, and does not declare the date struct, so is it not necessary to declare the date struct? Certainly not, notice. Inside the student struct, I have made a declaration of the date struct and declared the variable to be bri_day.

of course, similar types of structs have pooled unions and enumerated enums, and the usage is very similar, and after understanding the structure, look at them as small case.

Simple study c--fifth day

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.