structural morphology
Note: the name of structure type is a combination of a keyword struct and structure name (such as struct Student). The structure name is specified by the user, also known as "structure tag", to distinguish it from other structure types. In the above structure declaration, Student is the structure name (structure tag)
explain
struct structure name
{member list}
Type name member name;
A member list is also called a domain table
struct Student { int num; char name[20]; char sex; int age; float score; }
Note:
- If the member itself belongs to a structure type, you need to use several member operators to find the lowest level member level by level. Only the lowest level members can be assigned or accessed and operated.
- Structural variables of the same kind can be assigned to each other
student1 = student2 - You can refer to the address of the structural variable member or the address of the structural variable
scanf("%d",&student1.num); printf("%o",&student1);
Declare and define structure type variables
1. Separation of declarations and definitions
Declare structure type
sturct Student student1,student2;
Structure definition
student1.num = 10001; student1.name = "zhangxin"; //This assignment is incorrect here. Please tell Baidu how to assign the char array correctly. student1.sex = "M"; student1.age = 19; student1.score = 90.5; student2.num = 10002; student2.name = "wangli"; //This assignment is incorrect here. Please tell Baidu how to assign the char array correctly. student2.sex = "F"; student2.age = 20; student2.score = 98;
2. Declaration and definition shall be made at the same time
struct Student { int num; char name[20]; char sex; int age; float score; }student1,student2;
Note: the general form of this definition method is
struct Structure name {Member list }Variable name list;
3. Define directly without specifying the type name
struct {Member table column }Variable name table column;
explain:
- Only structural variables can be assigned, not structural types. At compile time, the structure class does not allocate space, and only allocates space to variables.
- A member in a struct type can have the same name as a variable in a program, but they do not represent the same object. For example, another variable num can be defined in the program, which is different from num in struct Student and does not interfere with each other.
Initialization and reference of structure variables
[example 9.1] put a student's information (including student number, name and gender) into the same structural variable, and output the student's information
#include<stdio.h> int main() { struct Student { int num; char name[20]; char sex; int age; // float score; }a={10101,"Li Lin",'M'}; printf("Student number:%d,full name:%d,Gender:%d. \n",a.num,a.name,a.sex,a.age); return 0; }
Define structure array
- sturct structure name
{member list} array name [array length];struct Person { char name[20]; int count; }leader[3];
- Structure type array name [array length];
struct Person leader[3];
Define structure array
[example 9.3] there are three candidates, and each voter can only vote for one person. It is required to compile a procedure for counting votes, first input the name of the candidate, and finally output the results of each person's vote.
#include<string.h> #include<stdio.h> struct Person() { char name[30]; int count; }leader[3]={"Li",0,"zhang",0,"sun",0}; int main() { int i,j; char leader_name[20]; for(i=1;i<=10;i++) { scanf("%s",leader_name); for(j=0;j<3;j++) { if(strcmp(leader_name,leader[j].name==0)) leader[j].count++; } } printf("\nResult:\n"); for(i=0;i<3;i++) printf("%5s:%d\n",leader[i].name,leader[i].count); return 0; }
Pointer to structure variable
struct Student *pt
[example 9.5] output the member information in the structure variable through the pointer variable pointing to the structure variable
#include<stdio.h> #include<string.h> int main() { struct Student { long num; char name[20]; char sex; float score; }; struct Student stu_1; return 0; }
Note: the following three are equivalent
- Stu member name (such as stu.num)
- (*p). Member name (e.g. * P) num)
- P - > member name (e.g. p - > Num)
Pointer to structure
[example 9.6] the information of three students is put in the structure array, and it is required to output the information of all students
#include<stdio.h> struct Student { int num; char name[20]; char sex; int age; }; struct Student stu[3]={{10101,"Li",'M',18}, {10102,"zhangfang",'M',19},{10104,"wangmin",'F',20}}; int main() { struct Student *p; printf("No. Name sex age\n"); for(p=stu;p<stu+3;p++>) { printf("%5d %-20s %2c %4d\n",p->num,p->name,p->sex,p->age); } return 0; }
Note:
- If the initial value of p is stu, that is, it points to the element with sequence number 0 of stu. After p Plus 1, p points to the next element. For example:
(+ + p) - > num / / first add 1 to p, and then get the value of num member in the element pointed to by p (i.e. 10102)
(P + +) - > num / / first obtain the value of P - > num (i.e. 10101), and then make p add 1 to point to stu [] - The p pointer is used to point to an object of type struct Student. It should not point to a member of the stu array element.
p = stu[1].name;//Illegal, stu[1] Name is the address of the first character of the member name in the stu[1] element. A "warning" message will be given during compilation, indicating that the type of address does not match.
- If a member address is assigned to P, for example, P = (struct Student *) stu [0] name; At this point, the value of P is stu [0] Name the starting address of the member. But p still keep the original type. If printf ('% s', P + 1) is executed;, The value of name in stu[1] will be output. When p + + is executed, the increment of the value of P is the length of the structure struct Student.
Use structural variables and pointers of structural variables as function parameters
struct Student { int num; char name[20]; float score[3]; float aver; }
1. Structural variables as function parameters
void input(struct Student stu);
2. Use the pointer / array of structure variables as function parameters
void input(struct Student stu[]);
Pointer processing linked list
Note: Tan Haoqiang version of C programming has section 9.4 "dealing with linked list with pointer", which I classified into Chapter 8 pointer
Common body
Format:
union common name
{member table column} variable table column;
for example
union Data { int i; char ch; float f; }a,b,c;
Note: the memory length occupied by structure variables is the sum of the memory length occupied by each member. Each member has its own memory unit. The memory length occupied by the common body variable is equal to the length of the longest member.
Reference common body variable
a.i a.ch a.f
characteristic
- The same memory segment can be used to store several different types of members, but only one member can be stored at each instant, rather than several at the same time.
Note: integer cannot be output in floating-point format, so printf ('% f', A.F.); The output is 0union Date { int i; char ch; float f; }a; a.i = 97; printf("%d",a.i); //Output 97 printf("%c",a.ch); //Output a printf("%f",a.f); //Output real number 0.00000
- For common massing, but the initialization table can only have one constant
union Date { int i; char ch; float f; }a = {1,'a',1.5}; //Error here
- The active member in the common body variable is the member assigned the last time. After assigning a value to a member in the common body variable, the value in the original variable storage unit is replaced.
- The address of a common body variable is the same as that of its members.
- You cannot assign a value to a common variable name or attempt to reference a variable name to get a value.
a = 1; m = a;
- Before C99, common body variables are not allowed to be used as function parameters. Function parameters can only be made in the form of pointers. After C99, they are allowed.
Note: the common body type is generally used when the members of two groups of data are mostly the same. For example, there are two sets of data of students and teachers. The members of students include: name, number, gender, occupation and class. Members of teachers include: name, number, gender, occupation and position. The two sets of data differ only in class and job, so they can be defined as the following common body:
struct { char name[20]; char sex; char job; union { int class; char position[10]; }category; }person[2];
Enumeration type
Note: enumeration types are listed in Chapter 3 "sequential programming"
typedef declares a new type name
Note: typedef declares the new type name in Chapter 3 "sequential programming"