Structure 1 - why do you need a structure? What is a structure?
First learn the important, second important, and then make up; The structure of C language is helpful to learn Java classes
Listen carefully in class and be efficient, otherwise you can't do anything
When learning, we should think: what is what, why and what is the use
Each line of the structure code must be understood. The structure experience affects the learning of the next two courses,
Data structure and object-oriented are the links between object-oriented and process-oriented
1. Why do I need a structure
In order to represent some complex things, ordinary basic types can not meet the actual requirements
2. What is a structure
A new composite data type formed by combining some basic types of data is called a structure
# include <stdio.h> struct Student //Structure to define a new data type - student type { int age; float score; char sex; }; int main(void) { struct Student st = {80, 98, 'A'}; //Define variables return 0; }
3. How to define a structure
Three methods, the first is recommended
//The first way is to define a new data type without defining variables struct Student //Structure to define a new data type - student type { int age; float score; char sex; }; //In the second way, variables are defined at the same time struct Student2 { int age; float score; char sex; } st2; //Notice the semicolon at the end //The third method does not specify the type name struct { int age; float score; char sex; } st3; //Notice the semicolon at the end
Structure 2 - how to use structure variables
1. Assignment and initialization
The initial value can be assigned as a whole while defining
After definition, you can only assign initial values individually
# include <stdio.h> struct Student //Structure to define a new data type - student type { int age; float score; char sex; }; //Notice the semicolon at the end int main(void) { struct Student st = {80, 66.6, 'F'}; struct Student st2; st2.age = 10; //st2 has been defined and can only be assigned individually st2.score = 88; st2.sex = 'B'; printf("%d %f %c\n", st.age, st.score, st.sex); printf("%d %f %c\n", st2.age, st2.score, st2.sex); return 0; }
2. How to extract each member in a structure variable (key)
(1) Structure variable name. Member name:
(2) Pointer variable name - > member name: pointer variable name - > member name
Inside the computer, it will be converted into (* pointer variable name). Member name
The above is the meaning of "-" > ", which is a hard and fast rule
The above two methods are equivalent
# include <stdio.h> struct Student //Structure to define a new data type - student type { int age; float score; char sex; }; //Notice the semicolon at the end int main(void) { struct Student st = {80, 66.6f, 'F'}; struct Student *pst = &st; //&st cannot be changed to st pst -> age = 88; //The second way // st.age = 88; // The first way st.score = 66.5f; /* 66.6 In C language, the default is double, If you want a real number to be of type float, you must add f or F to the end Therefore, 66.6 is double type and 66.6f or 66.6f is float type */ printf("%d %f\n", st.age, pst -> score); return 0; }
Above program notes:
(1) PST - > age will be converted into (* pst).age inside the computer. Why not
This is the meaning of - > and it is also a hard and fast rule
(2) So PST - > age is equivalent to (* pst).age is also equivalent to st.age
(3) We know that PST - > age is equivalent to st.age because PST - > age is converted into (* pst).age for execution
(4) Meaning of PST - > age:
The age member in the structure variable pointed to by pst
3. Structure variable and structure variable pointer are passed as function parameters
It is recommended to use structure pointer variables as function parameters
/* 2021 November 7, 2012 12:29:54 The input and output of structural variables are completed through functions */ # include <stdio.h> # include <string.h> struct Student { int age; char sex; char name[100]; }; //Semicolon cannot be omitted void InputStudent(struct Student *); void OutputStudent(struct Student ss); int main(void) { struct Student st; InputStudent(&st); //The address to which st must be sent when entering a structure variable // printf("%d %s %c\n", st.age, st.name, st.sex); // Here, the output string is% s and the output character is C, otherwise nothing can be printed OutputStudent(st); //Output the address of st to the structure variable, or send st directly /* The sending address is unsafe. Sometimes the called function is misoperated, and some programs will modify it, (C++)const can be used, read-only and unchanged However, in order to improve the sending speed and reduce memory consumption, the sending address is recommended */ return 0; } void InputStudent(struct Student * pstu) //pstu occupies 4 bytes { (*pstu).age = 10; strcpy(pstu->name, "Zhang San"); pstu->sex = 'F'; } void OutputStudent(struct Student ss) { printf("%d %s %c\n", ss.age, ss.name, ss.sex); }
Send address or send content:
One of the advantages of pointers: fast data transfer Low memory consumption Fast execution speed
4. Operation of structure variables
Structural variables cannot be added, subtracted, multiplied or divided
However, structural variables can be assigned to each other
struct Student { int age; char sex; char name[100]; }; // Semicolon cannot be omitted struct Student st1, st2; st1 + st2 st1 * st2 st1/st2 It's all wrong st1 = st2 Or st2 = st1 All right
5. Example: dynamically construct a structure array for storing student information
(1) Bubble sorting: sorting is more important than searching. It's easy to check when it's arranged. It's more complex
How to view the program: 1 process, 2 statement functions, 3 trial numbers
Read a book and look into it. Learning is very interesting. We should learn in this way
Who is plain sailing? You can make people look effortless only if you try your best
# include <stdio.h> //Bubble sorting void sort(int * a, int len) { int i; int j; int t; for(i=0; i<len-1; ++i) //There are six numbers, five rounds for(j=0; j<len-1-i; ++j) //There are n numbers to compare n-1 times { if(a[j] > a[j+1]) // >Indicates ascending order, < indicates descending order { t = a[j]; a[j] = a[j+1]; a[j+1] = t; } } } int main(void) { int a[6] = {10, 2, 8, -8, 11, 0}; //Array to sort int i; sort(a, 6); //Call the function to perform bubble sorting for(i=0; i<6; ++i)//Print out the sorted array printf("%d ", a[i]); return 0; }
Bubble sorting diagram: how to view the program: 1 process, 2 statement functions, 3 trial numbers
(2) Dynamically construct a structure array for storing student information
Dynamically construct an array to store student information, and then sort and output by score
# include <stdio.h> # include <malloc.h> //Construct the age, score and name of the structured student struct Student { int age; float score; char name[100]; }; //Note the semicolon //Main function int main(void) { //Define variables int len; struct Student * pstu; int i, j; struct Student t; //Dynamically construct one-dimensional arrays printf("Please enter the number of students:\n"); printf("len = "); scanf("%d", &len); pstu = (struct Student *)malloc(len * sizeof(struct Student)); //input for(i=0; i<len; ++i) { printf("Please enter page%d Student information:\n", i+1); printf("age = "); scanf("%d", &pstu[i].age); printf("score = "); scanf("%f", &pstu[i].score); printf("name = "); scanf("%s", pstu[i].name); /* name Is the name of the array, which itself is the address of the first element of the array Therefore, pstu[i].name cannot be changed to & pstu[i].name */ } //Bubble algorithm for sorting students' grades in ascending order for(i=0; i<len-1; ++i) for(j=0; j<len-1-i; ++j) { if(pstu[j].score > pstu[j+1].score) { t = pstu[j]; pstu[j] = pstu[j+1]; pstu[j+1] = t; } } printf("\n\n The student's message is:\n"); //output for(i=0; i<len; ++i) { printf("The first%d Student information:\n", i+1); printf("age = %d\n", pstu[i].age); printf("score = %f\n", pstu[i].score); printf("name = %s\n", pstu[i].name); printf("\n"); } return 0; }