Recently, I have carefully studied some of the Structure Knowledge and summarized the content. I hope my article will describe the character C structure that is fuzzy to the structure.
1. struct is a custom data type. It is also a large container used to store multiple data. However, struct is more flexible than array, and it can store different types of data.
2. struct Definition
Struct + struct name {fill in struct members in braces, and separate multiple struct members by semicolons}
As follows:
Struct student {
// Struct Member
Char name [20]; // storage name
Char sex; // store gender
Int number; // storage student ID
Int score; // store the score
}; // A semicolon indicates the end of the struct definition and is required
3. Type redefinition of the typedef type, that is, a new name is given to an existing type. The new name has the same function as the original type;
Typedef: first defines the struct type, and then typedef
For example, typedef int integer;
Redefines a name Stu for the struct type struct student
Typedef struct student Stu;
Typedef second format: when defining struct, typedef (this format is commonly used)
Typedef struct point1 {
Float abscissa; // store the abscissa
Float ordinate; // stores the ordinate values.
} Poi1;
Poi1 can then be used as a data structure type.
For example, poi1 P1 ={}; defines a struct type variable p1
4. The structure variable accesses the structure members through the structure variable. structure member,
For example: printf ("number = % d \ n", Stu. number );
5. struct array:
// Define a student struct variable
Stu person [4] = {"Frank", 'F', 87}, {"Duck", 'F', 93}, {"Xiaoming ", 'F', 27,10088, 93 },{ "phone", 'F', 25,10089, 75 }};
// Sort students by score using the struct array:
For (INT I = 0; I <4; I ++ ){
If (person [I]. score> scoremax. Score ){
Scoremax = person [I];
}
If (person [I]. age <agemin. Age ){
Agemin = person [I];
}
}
6. Structure memory allocation principle
It is allocated in units of the maximum size of the bucket specified by struct members.
Minimum integer multiple of the maximum space
7. struct can be nested, that is, the struct member in a struct is a variable of another struct type.
For example, first define a date struct
Typedef struct birthday
{
Int year;
Int month;
Int Day;
} Birth;
Then: the struct is nested. The struct member in a struct is a variable of another struct type.
Typedef struct Stu {
Char name [20]; // store the Student name
Char sex; // store Student gender
Int age; // specifies the student age.
Int score; // store student scores
Birth birth; // Date of birth
} Stu;
8. exercise:
1) define a struct variable (including year, month, and day) and calculate the day of the year as the day of the year? A days function is required. The parameter is a variable of this struct type, and the return value is an integer.
Int days (date dat)
{
Int A [12] = {0, 31, 59,90, 120,151,181,212,243,273,304,334 };
Int day = 0;
Bool istrue; // used to determine whether the year is a leap year
// Determine whether the year is a leap year
If (dat. Year % 400 = 0 | (dat. Year % 4 = 0 & dat. Year % 100! = 0 )){
Istrue = 1; // If the year is a leap year, set istrue to 1
} Else {
Istrue = 0;
}
Day + = A [dat. Month-1] + dat. Day;
Day + = istrue & dat. Month> = 3? 1: 0;
Return day;
}
2) five students are saved in the struct array. The program is sorted in descending order by the student's name.
Void sortname (stu a [], int N)
{
For (INT I = 0; I <n-1; I ++ ){
For (Int J = 0; j <n-1-I; j ++ ){
If (strcmp (A [J]. Name, a [J + 1]. Name) <0 ){
Stu temp = A [J]; // temp is an intermediate variable used to exchange two struct elements.
A [J] = A [J + 1];
A [J + 1] = temp;
}
}
}
}