Structural body
A struct is a custom type of data
struct Structure Body name
{
type specifier member name;
...
type specifier member name;
};
#import <Foundation/Foundation.h>intMainintargcConst Char*argv[]) { structteacher{Charname[ -]; Charsex; intAge ; Charcourse[ -]; }; typedefstructteacher teacher; Teacher Cui={"Cui",'m', -,"language C"}; structcup{floatPrice//Price intcapacity;//capacity Charcorlor[ -];//Color }; typedefstructCup Cup; Cup Fuguang={10.5, -,"Black"}; return 0;}
struct variable definition
A variable modified by a struct type is called a struct variable struct struct body name variable name ={initial value};struct Student stu1 = {1, "Zhangsan", ' m ', 70}; Struct member's access struct variable name. Member Variable name
EG:STU1.NUM//STU1 's School Number
Note: Struct member variables, like normal variables, can be assigned a valueanonymous structstruct declaration is combined with the definition of a variable eg:struct{?
int num;?
Char name[20];? char sex;? float score;?
}
STU1 = {1, "Wukong", ' m ', 99.5f},
STU2 = {2, "Bajie", ' m ', 65.0f};
//struct cup{//float price;//Price//int capacity;//capacity//Char corlor[20];//Color// };//typedef struct Cup cup; //defines a struct as an alias. //typedef struct cup{//float price;//Price//int capacity;//capacity//Char corlor[20];//Color//} Cup;
Exercise: There are three students, who find the highest score and the youngest.
typedefstructstudents{Charname[ +]; intAge ; floatscore; } Students; Students S1={"Zhangsan", -, the}; Students S2={"Lisi", -, the}; Students S3={"Wangwu", +, the}; Students Max= {0}; Max= S1.score>s2.score?s1:s2; Max= Max.score>s3.score?Max:s3; printf ("%s has the highest score \ n", Max.name); Students min= {0}; Min= S1.age<s2.age?s1:s2; Min= Min.age<s3.age?Min:s3; printf ("minimum age for%s", min.name);
Structure Space occupancy
Allocation of the maximum member variable type as the allocation unit I is allocated from top to bottom by struct member declaration order
Note: Allocating a new unit of space when allocating space is not sufficient to store member variables
Structure nesting
Members of a struct can still be structs
typedef struct date{int year;?
int month;?
int day; } mydate;
struct student{?
Char name[20];?
MyDate birthday;//stu1.birthday.year;
};
struct array
Placing multiple struct variables in an array, constituting a struct array
Eg:struct student students[10]={0};
//practice using a struct array to dotypedefstructdate{intYear ; intmonth; intDay ; } mydate; typedefstructstudents{Charname[ +]; intAge ; floatscore; MyDate birthday; } Students; Students stus[3]={ {"Zhangsan", -, the,{1989,5, -}}, {"Lisi", -, the,{1990,Ten,9}}, {"Wangwu", +, the,{1992,5,4}} }; Students min= stus[0]; for(inti =1; i<3; i++) { if(min.age>stus[i].age) {min=Stus[i]; }} printf ("%s\n", Min.name); Students Max= stus[0]; for(intI=1; i<3; i++) { if(max.score<Stus[i].score) {Max=Stus[i]; }} printf ("%s", max.name);
To the above student grades from the big to the small sort
for(intI=0; i<2; i++) { for(intj=0; j<2-I.; J + +) { if(stus[j].score>stus[j+1].score) {Students temp=Stus[j]; STUS[J]=stus[j+1]; Stus[j+1]=temp; } } } for(intI=0; i<3; i++) {printf ("%s%d%.2f\n", Stus[i].name,stus[i].age,stus[i].score); }
Sort write to function inside
typedefstructdate{intYear ; intmonth; intDay ;} Mydate;typedefstructstudents{Charname[ +]; intAge ; floatscore; MyDate birthday; } Students;voidBobblesart (Students s[],intcount) { for(intI=0; i<count-1; i++) { for(intj=0; j<count-1-I.; J + +) { if(s[j].score>s[j+1].score) {Students temp=S[j]; S[J]=s[j+1]; S[j+1]=temp; } } }}
Summarize
Structs are a flexible type of data structure and are similar to the classes that OC wants to learn
Structures and arrays can be nested together to achieve more complex data structures