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
1 /*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*/2#include <stdio.h>3 4 structTest//defining the number of struct bodies5 {6 intP//Save character Occurrences7 CharQ//Save character Types8 };9 structTest array[4];Ten One voidPaixu (structTest *b,intn);//calling a defined function A - intMain () - { the - 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 it - Charn[ -]; +printf"input string: \ n"); -scanf"%s", n);//reading the input string + A for(intI=0;i< -; i++)//calculate the number of occurrences of a,b,c,d with a for loop at { - if(n[i]=='a'|| n[i]=='A') - { -a++; - } - in Else if(n[i]=='b'|| n[i]=='B') - { tob++; + } - Else if(n[i]=='C'|| n[i]=='C') the { * $C++;Panax Notoginseng } - Else if(n[i]=='D'|| n[i]=='D') the { +d++; A } the + } - //assigning values to struct variables $array[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'; -Paixu (Array,4);//Call sort function - for(intL=0;l<4; l++)//Cyclic output Display the { - Wuyiprintf"%c Number of occurrences is%d\n", ARRAY[L].Q,ARRAY[L].P); the } - return 0; Wu } - About $ - voidPaixu (structTest *b,intN//Create a sort function - { - intTemp//defines an int type variable for use in numeric Exchange A CharTemp2;//defines a char type variable for character Exchange + for(intI=0; i<n;i++)//For Loop traversal the for(intj=0; j<n-i;j++)//sort ABCD in descending order - { $ if(b[j].p<b[j+1].P) the { theTEMP=B[J].P;//If the previous character appears less than the last one to be swapped theb[j].p=b[j+1].P; theb[j+1].p=temp; -TEMP2=B[J].Q;//Character type Exchange inb[j].q=b[j+1].q; theb[j+1].q=Temp2; the About } the the } the +}
The use of the structure in the--c language of Dark Horse programmer and the application of precautions and practice