1, Basic knowledge
1. Definitions
Structure is a data set composed of a series of data of the same type or different types, also known as structure.
The data in the structure is called member variables.
Member variables can also be arrays, pointers, and structures.
2 declaration
struct student
{
char name[20]; / / name
int number; / / student number
int score; / / achievement
};
1. First write the keyword struct , and then write the structure name , student (this name can be chosen by yourself)
2. The things in curly braces {}, {} are called code blocks, which contain member variables
3. Define member variables For example: char name [20]
4. Add a semicolon} after the last curly bracket;
3. Definition and initialization of structure variables
(1) Structure variables are defined when a structure is declared
struct student
{
char name[20]; // full name
int number;// Student number
int score; // achievement
}stu1={"zhang san" , 111111,100}; / / initialize directly after defining the structure variable
(2) Declare the structure first, and then define the structure variable separately
struct student
{
char name[20]; // full name
int number;// Student number
int score; // achievement
};
struct student stu1={"zhang san" , 111111,100}; ; / / define structure variables separately and initialize them (this sentence should be in the main function)
(3) The least recommended method is not to write struct names
struct
{
char name[20]; // full name
int number;// Student number
int score; // achievement
char sex; // Gender
}stu1={"zhang san" , 111111,100,'M'};
Note: when initializing structure variables, the data is separated by commas. When the data is a string, it is enclosed in double quotation marks; When the data is a character, enclose it in single quotation marks; The data is numbers. Just write it directly.
4. Access to structure members
Use the dot operator "." structure variable name. Member variable, such as stu1.number
#include<stdio.h> struct student { char name[20]; //full name int number;//Student number int score; //achievement }stu1; //Structure variables are defined when a structure is declared int main () { stu1.number =11111; //Student number to access stu1 printf("%d",stu1.number ); return 0; }
2, Structure array
Define structure array and initialize
Just like} defining and initializing structure variables, you only need to change the variable name into an array name.
Compare it
(1) Structure variables are defined when a structure is declared
struct student
{
char name[20]; // full name
int number;// Student number
int score; // achievement
}stu1={"zhang san" , 111111,100}; / / initialize directly after defining the structure variable
Let's look at the structure array
(1) Define a structure array while declaring a structure
struct student
{
char name[20]; // full name
int number;// Student number
int score; // achievement
}stu[3]={ {"zhang san" , 111111,100},
{"li si" , 111112,99 },
{"wang wu" , 111113,98} }; / / initialize directly after defining the structure array
As shown in the figure
Difference: the definition of structure array is completed by changing the structure variable stu1 , into structure array stu[3]. Other methods of defining structure variables are also applicable to , defining structure array.
Initialization: the initialization of structure array is equivalent to the initialization of multiple structure variables,
Note: when initializing the structure array, each array element should be enclosed by a {}, and each two array elements should be separated by commas "," and finally the whole should be enclosed by a {} with a semicolon "
3, Structure pointer
1. Pointer to structure variable
The general form of defining structure pointers is as follows
Structure type * pointer name
For example: struct student *p
There are two ways to access members with structure pointers:
One is to use the dot operator ".". Such as (* p) Member name} note: be sure to enclose * p in parentheses
One is to use the pointing operator "- >". E.g. p - > member name
#include<stdio.h> struct student { char name[20]; //full name int number;//Student number int score; //achievement }stu1; //Structure variables are defined when a structure is declared void print(struct student* p) { printf("%d\n", (*p).number); //You can use some printf("%d\n",p->score ); //You can also use "- >" to point to operators and members } int main () { struct student stu1={"zhangsan",111111,100}; print(&stu1); return 0; }
2. Pointer to structure array
The structure pointer can point not only to the structure variable, but also to the structure array. At this time, the value of the pointer variable is the first address of the structure array.
For example, a structure array stu[3] has been defined earlier, and the structure pointer is used to point to this array
struct student *p;
p=stu; / / at this time, it refers to the first address of the array
//You can also point directly to the address of an element in the array
p=&stu[2]; / / just add the address fetching symbol & and the subscript [2]
#include<stdio.h> #include<string.h> struct student { char name[20]; //full name int number;//Student number int score; //achievement }stu[3]={ {"zhang san" , 111111,100}, {"li si" , 111112,99 }, {"wang wu" , 111113,98} }; int main () { struct student *p1,*p2; p1=stu; //Point to the first address of the array p2=&stu[2];//Points to an element in an array printf(" %s , %d ,%d \n",p1->name ,p1->number ,p1->score ); printf(" %s , %d ,%d \n",p2->name ,p2->number ,p2->score ); return 0; }
Pointer variables can also be added or subtracted
#include<stdio.h> #include<string.h> struct student { char name[20]; //full name int number;//Student number int score; //achievement }stu[3]={ {"zhang san" , 111111,100}, {"li si" , 111112,99 }, {"wang wu" , 111113,98} }; int main () { struct student *p1; p1=stu; int i; for(i=0;i<3;i++,p1++) { printf(" %s , %d ,%d \n",p1->name ,p1->number ,p1->score ); } return 0; }
Note: (+ + p) - > name is to execute the + + operation first, make p point to the address of the next element, and obtain the member value of the element
(p + +) - > name is to perform the pointing operation first, make p point to the address of this element, obtain the member value of this element, and then make p point to the address of the next element.
3. Structure as function parameter
1). Use structure variables as function parameters
This is the use of value passing, which will pass all the contents of the structure variable to the formal parameter. The formal parameter must also be a structure variable of the same type. Moreover, if the value of the structure variable member is changed in the formal parameter, the changed value will not be returned to the main function.
For example: void Display(struct student stu)
(because the value transfer is one-way, it can only be transferred from the argument to the formal parameter, not from the formal parameter to the actual parameter. To change the value of the structure variable in the formal parameter and return it to the main function, you need to use a pointer, which will be explained later.)
#include<stdio.h> #include<string.h> struct student { char name[20]; //full name int number;//Student number int score[3]; //achievement }stu= {"zhang san" , 111111,100,99,98}; void Display(struct student stu) //Calculate average score { printf("score 1 : %d\n",stu.score [0]); printf("score 2 : %d\n",stu.score [1]); printf("score 3 : %d\n",stu.score [2]); printf("average score : %d",(stu.score [0]+stu.score [1]+stu.score [2])/3); } int main () { Display(stu); return 0; }
2). Use {structure pointers as function parameters
The structural variable is used as the function parameter. The value of the variable member changed in the formal parameter cannot be returned to the main function. Now use the pointer to pass it
For example: void Display(struct student *p)
#include<stdio.h> #include<string.h> struct student { char name[20]; //full name int number;//Student number int score[3]; //achievement }stu= {"zhang san" , 111111,100,99,98}; void Display(struct student *p) { printf("score 1 : %d\n",(*p).score [0]); p->score [0]=97; //change data } int main () { struct student *p; p=&stu; Display(p); //Because the formal parameter is in the form of pointer, you only need to write "p" to represent the address, not "* p" printf("score 1 : %d\n",p->score [0]); //After the change, the data is returned to the main function return 0; }
4. Structure including structure
A structure member type can be not only a basic type, but also a structure type
For example, define a structure to represent student information, including name, student number, date of birth, and the date belongs to a structure type, including month, year and day. In this way, the structure of student information includes the structure of date
#include<stdio.h> #include<string.h> struct date { int year;//year int month;//month int day;//day }; struct student { char name[20]; //full name int number;//Student number struct date birthday; //date of birth }stu= {"zhang san",111111,{2003,2,6}}; int main () { //Output student information printf("name : %s\n",stu.name ); printf("number : %d\n",stu.number ); printf("birthday : %d %d %d\n",stu.birthday.year,stu.birthday.month,stu.birthday.day); return 0; }
1). Because birthday is a structure, you should use {} to enclose the data when assigning values
2). stu.birthday.year represents the value of the member year in the structure birthday in the structure stu (decreasing step by step until the lowest level)