1. Structure and Array differences
① struct: A struct can consist of several different types of data, which can contain multiple types, such as int double
② Arrays: Arrays can only consist of multiple identical types of data
2. Definition of structure
① define struct type first
struct person{int//ageHeight name three variables can be called struct members or attribute double height; Char*name;
② defining struct-body variables based on struct type
struct Person p={,1.75,"Tom"};p.age =;p. Name ="Tim"// re-assigning the struct variable
3. Structure in vivo analysis
①
struct // is to define the struct type, and no storage space is allocated {intdouble height; Char*name;
②struct person p={20,1.75, "Tom"}; Defining struct-body variables is really allocating storage space
The storage space occupied by the ③ struct must be a multiple of the maximum number of member bytes
The int type occupies 4 bytes, the char type occupies 8 bytes, and sizeof (person) occupies 16 bytes (8*2) instead of 12 bytes (8+4)
4. Define the variable mode
① define the type first, and then define the variables, such as those described above
② definition types define variable examples as well:
struct student{int age ; Char *name; Stu;
③ defining a variable at the same time
struct // Type Nameless Called this definition method cannot be reused {int age ; Char *name; Stu;
5. Pointers to struct bodies
① definition: struct student *p;
② using pointers to access members Chengyuan
1> (*p) member name
2> p-> member name
struct student{int age ; int No;} // struct-Body variables struct student stu= (1); // pointer variable p future pointer to struct student type data struct student *p; // pointer variable p points to stu variable p=&stu;p->age=;
Three ways to Output
ⅰprintf ("age=%,no=%d", stu.age,stu.no);
ⅱprintf ("age=%,no=%d", (*p). Age, (*p). No);
ⅲprintf ("age=%,no=%d", p->age,p->no); The most common method
Example
/*Enter a whole bunch of strings from the keyboard, count the number of occurrences of a,b,c,d, and the number of occurrences from high to low output letters and occurrences*/#include <stdio.h>structTest//defining the number of struct bodies{intP//Save character OccurrencesCharQ//Save character Types};structTest array[4];voidPaixu (structTest *b,intn);//calling a defined functionintMain () {inta,b,c,d; A=b=c=d=0;//defines ABCD four shaping variables for storing the number of occurrences of a string and initializing itCharn[ -];p rintf ("input string: \ n"); scanf ("%s", n);//reading the input string for(intI=0;i< -; i++)//calculate the number of occurrences of a,b,c,d with a for loop{if(n[i]=='a'|| n[i]=='A') {a++;}Else if(n[i]=='b'|| n[i]=='B') {b++;}Else if(n[i]=='C'|| n[i]=='C') {C++;}Else if(n[i]=='D'|| n[i]=='D') {D++;}}//assigning values to struct variablesarray[0].p=a;array[1].p=b;array[2].p=c;array[Geneva].p=D; array[0].q='a'; array[1].q='b'; array[2].q='C'; array[3].q='D';p Aixu (Array,4);//Call sort function for(intL=0;l<4; l++)//Cyclic output Display{printf ("%c Number of occurrences is%d\n", ARRAY[L].Q,ARRAY[L].P);}return 0;} voidPaixu (structTest *b,intN//Create a sort function{intTemp//defines an int type variable for use in numeric ExchangeCharTemp2;//defines a char type variable for character Exchange for(intI=0; i<n;i++)//For Loop traversal for(intj=0; j<n-i;j++)//sort ABCD in descending order{if(b[j].p<b[j+1].p) {temp=B[J].P;//If the previous character appears less than the last one to be swappedb[j].p=b[j+1].p;b[j+1].p=TEMP;TEMP2=B[J].Q;//Character type Exchangeb[j].q=b[j+1].q;b[j+1].q=Temp2;}}}
The use of the structure in the--c language of Dark Horse programmer and the application of precautions and practice