------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -----
The first structure array
First, the concept of an array of structural bodies
The elements of an array can also be of a struct type. This makes it possible to form a structured array. Each element of a structure array is the following table structure variable with the same structure type. In practical applications, a structure array is often used to represent a group with the same data structure. such as a class of student files, a workshop staff salary table and so on.
Second, the structure array definition
Define the format:
struct struct name {
Member table columns
} array name [array length];
For example: Define an array of length 5, where each element is a Stu struct type.
struct stu{
int num;
Char *name;
char sex;
Float score;
}BOY[5];
Defines an array of structures boy, a total of 5 elements, boy[0]~boy[4]. Each array element has a struct-stu form.
1/* 2 struct array: 3 4 is used to hold a large number of structural body variables of the same structure. 5 6 struct Array definition: 7 8 The first way that a struct array is defined: 9 1. Defines the structure of a struct and defines the array of student{11 int age;12 char *name;13 int sno;14 float score;15}stu[5];17 1 8 2. Define the structure first, then define the array Student boy[5];20 21 */22 23//second, initialize and traverse the structure array #include < STDIO.H>26 #include <string.h>27 int main (int argc,const char * argv[]) {28 29///A struct array initialization method 30 31//1. When you define an array of struct bodies, initialize the struct student{33 char name[20 ];35 int age;36}boy[3]={{"SB", 18},{"ZBZ", 38},{"CGX", 38}};37//2. Initialized at the same time as defined by the UCT Student girls[2]={{"Fengjie", 18},{"CJK", }};40 41 42//3. First define the post-initialization, the overall assignment of the struct Student ds[2 ];44//Ds[0 ]= (struct Student) {"xzmly", 18};45//ds[1]= (struct Student) {"Bdjy", 18};46 47//4. First define an array of structs, then initialize the struct Student stu[2< span>];49//stu[0].name = "Zhangsan"; Not strcpy (Stu[0].name, "Zhangsan"); The first method is scanf ("%s", stu[0].name); The second method is Stu[0].age = + ; 54 55//second, the structure of the array of traversal *******56 (int i =0;i<3;i++ ) {("Name:%s,age :%d\n ", boy[i].name,boy[i].age); }61 return 0 ; + /span>
1/* 2 think & implement 1:3 struct stu{4 int num, 5 char *name; 6 char sex; 7 Float score; 8}; 9 struct Stu boy[5]={10 {101, "Li ping", ' F ', 45},11 {102, "Zhang ping", ' M ', 62.5},12 {103, "He Fang ", ' F ', 92.5},13 {104," Cheng Ling ", ' M ', 87},14 {," Wang Ming ", ' M ', 58}15 16};17 1) using the above STU structure, computational science Number of students with average and failing grades 18 19 2) print 80-100 points of student's grade and name */21 #include <stdio.h>23 int main (int argc,const char *Argv[]) {a structstu{27 intnum;28 char *name;29 Charsex;30 floatScore;31};32 33//1. Defining an array of structural bodies Stu boy[5]= {101, "Li ping", ' F ',},36 {102, "Zhang ping", ' M ', 62.5 },37 {103, "He Fang", ' F ', 92.5 },38 {104 , "Cheng Ling", ' m ',}40 },39 {};42, "Wang Ming", ' m ', (+) ', ' 43//2. "There is a loop of//45 float sum = 0.0f ; int count = 0; Number of failed to save (int i = 0;i<5;i++ ) {48 49//To calculate total score sum+= boy[i].score;51//Determine whether the result is less than 60, if less than 60, to let the calculator +15 2 if (boy[i].score<60 ) {count++ ;}else if (boy[i].score>=80 && boy[i].score<=100 ) {55//determine if greater than 80 is less than 10056 57//If in this interval, the name and score should be output in printf ("Name:%s, score:%.2f\n" , Boy[i].name,boy[i].score); 59 }60 }61 ("Average:%.2f\n", Sum/5 ); ("Number of failed persons:%d\n" , Count); 0 ; 66 SPAN>}67 68/*69 Operation Result: 70 71 name: He Fang, score: 92.5072 Name: Cheng Ling, score: 87.0073 average: 69.0074 failed number: 275 Press any key to continue */
Third, the realization of simple address Book 2 thinking & implementation 1:3 1) using a struct array to implement contacts 4 5 #include <stdio.h> 6 #define LEN 5 7 8 struct
person{9 //definition array Save contact name Char name[21
];11 //Save phone number ( char telnum[12 }contacts[len]; A. int main (int argc,const char * argv[]) {17 18//1. First define structure 19 20//2. Define struct-body array struct person contacts[len];22 23//3. Let the user enter the contact information to be saved in printf ("Please enter the contact you want to save, format: name, phone \ n"); (int i=0;i<len;i++) {scanf ("%s%s") c19>}30//4. Once saved, check for the for (int j=0;j<len;j++) {The "Name:%s, Tel:%s\n" }35 0; PNS}
Second, structure pointer definition and initialization
A pointer to a struct variable
Derivation:
int num = 10;
int *p = #
P-->num
struct person S1;
struct Peron *p2 = &s1;
A pointer variable is called a struct pointer variable when it is used to point to a struct variable. The value in the struct pointer variable is the first address of the structure variable that is pointed to. The structure variable is accessible through the struct pointer, which is the same as the array pointer and the function pointer.
The general form of a struct pointer variable description is:
struct struct name * struct pointer variable name
/*
1. What is a struct pointer ?
pointer variable to hold the address of the struct variable
2. Structure pointer definition
struct struct body name * pointer variable name;
// Define a structure body
struct car{
int Lunzi;
int speed;
}car1;
struct Car *p=null; Defines a struct-body pointer variable
p = &car1;
p = &Car;// error It is a struct
Structural body name Car
struct variable name car1
struct-body pointer p
*/
Third speaking struct pointers indirectly accessing member values
A pointer to a struct variable
The general form of its visit is:
(* struct pointer variable). Member name
or for:
struct pointer variable-member name
For example:
(*pstu). Num
Or:
Pstu->num
1/* 2 1. Member value of struct variable 3 4 struct student{5 6 int age; 7 char *name; 8}; 9 10//Define a variable of a struct Student stu1={18, "Zhang Sanfeng"};13 14//struct variable member value has 2//stu1.age value 1816//stu1.name value Zhang Sanfeng 17 18 2. The member value of the struct variable is accessed indirectly by the struct pointer, Student *p = &stu1;21 22//using P Take 18 three Fung 23 242 ways: 25 26 1. (*p). Age Access Ages (*p)--->stu127 (*p). Name Access name 28 29 2. P->age30 p->name31 32 Note: p must be a struct pointer */34 #include <stdio.h>36 PNS int main (int argc,con St char *argv[]) {38 39//1. Member value of struct variable-- student{42 Ame };46 47//define a struct variable Student stu1={18, "Zhang Sanfeng"};50 51//struct variable member value has 2//stu1.a GE value 1853//stu1.name value Zhang Sanfeng 54 55//2. The member value of the struct variable is accessed indirectly by the struct pointer. Student *p = &stu1;58 printf ("Name:%s, Age:%d \ n ", (*p). Name, (*P). Age); (" Name:%s, Ages:%d\n ",p->name,p->); return 0;
Iv. Structure Nesting use
One, the structure of the use of nesting
( We have learned if nested
if () {
if () {
}
}
Trinocular operator
(?:)?:
For operator
for () {
for () {
}
}
Recursive functions
Max (Max (), 45);
)
1, members can also be a structure, that is, constitute a nested structure.
Structure nesting: There are other structures within the structure definition.
Structs cannot nest their own variables, and can nest pointers to their own type.
For example
struct date{
int month;
int day;
int year;
};
struct stu{
int num;
Char *name;
char sex;
struct Date birhday;
Float score;
};
1/* 2 structure nesting: 3 4 struct definition, member of struct is another structure variable 5 6 structure nesting considerations: 7 8 1. Variable 9 in struct definition that can be nested in other struct types You cannot nest yourself this type of variable, the struct date{12 int year;13 int month;14 int day;15 16};17//defines a student's structure of the struct Student {*name;20 int age;21 Float score;22//strcut Student Stu; Errors cannot be nested with their own type of variable//struct Student *stu; The correct nesting of a struct pointer of the current type is a struct Date birthday;25};26 27 2. You can nest your own type of pointer. */29 #include <stdio.h> int. int main (int argc,const char *Argv[]) {33 34//structure nested: 35 36///struct definition, member of struct is another struct variable 37 38//Structure Nesting Note: 39 40//1. Struct definitions can nest other struct types. Variable 41//Can not nest its own variable of this typetime{44 inthour;45 intmin;46 intsec;47};48//Defines a date structure for a structdate{50 intyear;51 intMonth intStructure of day;53//nested time structTime Time;55 56};57//Defines a student's structure.Student {* * char *name;60 intage;61 floatscore;62//Strcut Student Stu; Errors cannot be nested in their own type of variable//struct Student *stu; Correctly nested a struct pointer of the current type.Date birthday;65 };66 67//2. You can nest your own type of pointer.//struct Student *stu; The correct nesting of a struct pointer of the current Type 69 70//nested struct How to initialize the Student stu1={"Zhang Sanfeng", 28,59.99f,{1200,12,12,{12,12,12}}; Defines a struct variable 72 73//nested struct How to access the compact printf ("Name:%s, Age:%d (Birthday:%d-%02d-%02d), score:%.2f\n", Stu1.name,stu1.age,stu1.birthday.year,stu1.birthday.month,stu1.birthday.day,stu1.score); 75//Access time printf ( "Name:%s, Age:%d (Birthday:%d-%02d-%02d%02d:%02d:%02d), score:%.2f\n", Stu1.name,stu1.age,stu1.birthday.year, Stu1.birthday.month,stu1.birthday.day,stu1.birthday.time.hour,stu1.birthday.time.min,stu1.birthday.time.sec, Stu1.score); 77 78//3. The structure nested itself in the pointer of the person{81,name;83 int age;84//nested its own type of pointer----struct PERSO n *child;86};88// struct nested itself pointer, initialize 89//define KIM90 struct Person Kim = {"Kim", 8, NULL};//struct person *chil D = &kim;92 struct person p1 = {"Lin",38,&kim};93 94//struct nested itself pointer, access to the "%s son is:%s, son's age:%d\n", P1.name , (*p1.child). Name, (*p1.child). (*). printf ("The Son of%s is:%s, the age of the son:%d\n",p1.name,p1.child->name,p1.child-> (age ); return 0; 98}
Structure variables and members as function parameters
1. Parameter of member value function
struct member properties are values passed as arguments to the function (except that the member variables are arrays).
2. struct variable name as parameter of function
in the ANSI C standard, the function parameters of the structure variables are allowed to be used for the whole transmission. However, this kind of transmission is to transfer all the members one by one, especially when the members are in an array, the time and space of the transmission will be very high, which seriously reduces the efficiency of the program. So the last thing to do is to use pointers, that is, to transfer the function parameters with pointer variables. At this point, the argument is passed to the formal parameter only address, which reduces the time and space overhead.
1 #include <stdio.h> 2 3 struct car{4 5 int Lunzi; 6 int speed ; 7 }; 9 void Xiuche (i NT N) {each n =2}14 15//Use struct variable as the parameter of the function of the xiuche1 (struct}21 int main (int argc, const char * Argv[]) {22 23//define a struct variable for a struct Car car1={4,200 xiuche1 (CAR1); printf ("%d\n", Car1.lunzi);//432 return 0
;34}
The structure pointer as a function parameter
1 #include <stdio.h> 2 3 struct car{5 int lunzi; 6 int} 9//struct pointer as parameter of function ten//C1 is a struct pointer all VO ID xiuche2 (struct Car *C1) { C1->lunzi = 2}16 int main (int argc, const char *argv[]) {17 18//Define a structural body change Amount of struct Car car1={4,200};20 21//NOTE: The address of the struct variable is passed to the function 22///can also be understood as the parameter of the struct pointer as a function 23 24//Essence: Is the address passing the Xiuche2 (& CAR1); printf ("%d\n", Car1.lunzi); 227 return 0;
Dark Horse programmer-----Structure Array