First, the structural body
1) Disclaimer
Struct is used to declare the struct body
Role: Managing Multiple Data messages
struct student{ int num; // The member variables are separated by; int Age ; Char name[[]; float score;} Student; // semicolon Don't forget
2) Initialization
1. Set the initial value using {}
2. Assigning values in the order of member variables
3, can not set information, using {0}
Student stu1={1,"luofeng",90.0}; Student stu2={2,Bayi,"sunjude",99.9 }; Student stu3={0};
4, the same struct type variable can be assigned to each other [array name is not possible]
STU3 = STU1;
5, the structure variable definition, can not be a whole assignment, can only assign a value to the member variable alone
" Canglaoshi ");
printf ("%d,%d,%s,%.2f", Stu1.num,stu1.age,stu1.name,stu1.score);
3) typedef primitive type name new type name
1, typedef struct student Animal; Structure weight naming
float OK; int Main (intconstChar * argv[]) { ok a0.89; printf ("%f\n", a); return 0 ; }
4) Local Variables
1, defined within the function, the entire function is valid
2, scope: in {} valid, out of {} is released, can no longer use
3, different functions, you can define the same variables, each function has its own scope
4, defined in a code snippet, can only be used in code snippets, loops, branching
5, the variable is defined in the function, and then the same variable is defined in the code snippet [in the code snippet, used in a code snippet, outside the code snippet, using variables within the function]
5) Global Variables
1, function outside the definition, program run end release
2, starting from the position defined by the global variable, the following program can use the global variable
3. Global variables and local variables can be renamed
4. Set initial value using {}, can not set information, use {0}
5. Assigning values in the order of member variables
6) Structured arrays
1, the elements of the array are struct type
typedefstructstudent{Charname[ -]; intscore; intAge ;} Student;intMainintargcConst Char*argv[]) {Student stu[3]={{"Dawang", -, -},{"Xiaowang", -, -},{"Wangzha", the, -}}; Student Max= {0}; Student min= stu[0]; for(intI=0; i<3; i++) { if(Max.score <Stu[i].score) {Max=Stu[i]; } if(Min.score >Stu[i].score) {min=Stu[i]; }} printf ("%s%d%d\n", Max.name,max.score,max.age); printf ("%s%d%d\n", min.name,min.score,min.age);return 0;}
2. Sort by score
typedefstructstudent{Charname[ -]; intscore; intAge ;} Student;intMainintargcConst Char*argv[]) {Student stu[3]={{"Dawang", -, -},{"Xiaowang", -, -},{"Wangzha", the, -}}; for(inti =0; I <2; i++) { for(intj =0; j<2-I.; i++) { if(Stu[j].score < stu[j+1].score) {Student temp= {0}; Temp=Stu[j]; STU[J]= stu[j+1]; Stu[j+1] =temp; } }
} for(intI=0; I <3; i++) {printf ("%s%d%d\n", Stu[i].name,stu[i].score,stu[i].age); }return 0;}
7) Structure nesting
typedefstructdate{intYear ; intmonth; intDay ; }date;typedefstructstudent{Charname[ -]; intscore; intAge ; Date birthday;} Student;intMainintargcConst Char*argv[]) {Student Stu= {"Zhangsan", the, at,{2003, One, One}}; Stu.birthday.year= -; printf ("%d,%d,%d", stu.birthday.year,stu.birthday.month,stu.birthday.day);return 0;}
8) Function call struct
1,. h Code
struct teacher{ char name[); int Number ; int Age ; } Teacher; void Tiger (Teacher t1,teacher T2);//parameter declaration struct-body variable void Blog (Teacher tea[],int n);
2,. M code
voidTiger (Teacher t1,teacher T2) {Teacher max= t1.age > T2.age?t1:t2; printf ("%s%d%d\n", max.name,max.number,max.age);}voidBlog (Teacher tea[],intN) { for(inti =0; I < n1; i++) { for(intj =0; J < N-1-I.; J + +) { if(tea[j].age< tea[j+1].age) {Teacher temp; Temp=Tea[j]; TEA[J]= tea[j+1]; Tea[j+1] =temp; } } } for(intI=0; i<n;i++) {printf ("%s%d%d\n", Tea[i].name,tea[i].number,tea[i].age); }}
3. MAIN.M Code
Teacher Tiger1 = {"haha",2, at}; Teacher Tiger2= {"hehe",3, -}; Tiger (Tiger1, tiger2); Teacher tea[]={{"Dawang", -, -},{"Xiaowang", -, -},{"Wangzha", the, -}}; Blog (tea,3);
C-Language basic structure