Role: Create a new data type that can unload the inside or outside of a function
Structure:
struct struct body name {
Data type 1 member variable name 1;
Data type 2 member variable name 2;
// ...
// ...
Data type N member variable n;
//};
Naming conventions for structures: Large Hump method
Multiple member variables separated by semicolons;
The outer braces of the structure are appended with semicolons;
struct Student {
Short number;
Char name[20];
char sex;
Float score;
// };
To define a struct-body variable
struct struct name Variable name = {corresponding to each member variable disposition, separated by commas};
struct Student Student = {123, "Mike", ' F ', 99.9};
Get the value of a member variable
Variable name. Member variable name (point syntax)
Student.number;
printf ("%hd\n", Student.number);
Renaming a struct
/*
The wording of a
struct Rectangle {
int lon;
int width;
};
typedef struct RECTANGLE Rec;
*/
Two
typedef struct RECTANGLE {
int lon;
int width;
P Dian;
}r;
typedef struct STUDENT {
Short number;
Char name[20];
char sex;
Float score;
}stu;
Structure can be directly assigned to a value
struct Student stu1 = {1314, "guozong", ' M ', 99.9};
struct Student stu2 = {520, "qiaoming", ' M ', 9.99};
struct Student Stu3 = {0};
STU3 = stu1.score > Stu2.score? STU1:STU2;
printf ("number =%HD sex =%c name =%s score =%.2f\n", Stu3.number, Stu3.sex, Stu3.name, Stu3.score);
Structure Body Storage occupancy
Allocate units with the largest member variable type occupied space
Top-down allocation by struct member declaration order
Note: Allocating a new unit of space when allocating space is not sufficient to store member variables
struct array
struct Student students[5] = {0};
Stu Students[5] ={
{101, "AAA", ' F ', 88.0},
{102, "BBB", ' m ', 89.0},
{103, "CCC", ' F ', 90.0},
{104, "ddd", ' m ', 99.0},
{"Eee", ' F ', 97.0}
};
for (int i = 0; i < 4; i++) {
for (int j = 0; J < 4-i; J + +) {
if (Students[j].score < Students[j + 1].score) {
Stu temp = students[j];
STUDENTS[J] = students[j + 1];
Students[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
printf ("num =%hd name =%s Sex =%c score =%.1f\n", Students[i].number, Students[i].name, Students[i].sex, STUDENTS[I].S CORE);
}
C-language structural body