🌞 Welcome to the world of C language
🌈 Blog home page: Qingyun Pavilion
💌 Welcome to pay attention 🎉 give the thumbs-up 👍 Collection ⭐ Leave a message 📝
🌟 This article is original by Qingyun Pavilion!
🌠 This stage belongs to the forging stage. I hope you can make a breakthrough smoothly
📆 Starting time: 🌹 January 18, 2021 🌹
✉️ I hope I can complete the advanced road with you!
🙏 The author's level is very limited. If you find an error, please leave a message! Thank you very much!
catalogue
0 ️⃣ Definition and alias of structure type variable
one ️⃣ Structure variable initialization and reference
three ️⃣ Pointer to structure type
four ️⃣ Structure type data as function parameters
0 ️⃣✨✨✨ Definition and alias of structure type variable ✨✨✨
After defining a structure type, a new data type is generated, which can be used to define variables. There are three ways to define variables of structure type:
(1) Define the structure type first and then the variable
For example, a structure type is defined:
struct student { int num; //Student number char name[20]; //full name char addr[30]; //address };
After defining the structure type, you can define variables in the form of "type variable name". For example, struct , student , stuA,stuB;, This defines two variables stuA and stuB, both of which are of type struct} student.
(2) Define variables while defining types
struct student { int num; char name[20]; char addr[30]; }stuA,stuB;
In this way, two variables stuA and stuB of type struct student are also defined. After defining variables in this way, you can continue to define other variables of this structure type in the first way.
(3) Directly define structure type variables
As mentioned earlier when defining a structure type, the tag of a structure type can be absent, which is called anonymous structure type. To define variables of anonymous structure type, you can only define variables immediately after defining a structure type, and you can no longer define variables of this type in subsequent codes.
struct { int num; char name[20]; char addr[30]; }stuA,stuB;
C provides a keyword typedef, which can define the structure type as an alias, and then use this alias to define the variables of this structure type, instead of writing it in the form of "struct structure name and variable name". For example,
typedef struct student { long int num; char name[20]; char sex; char addr[20]; }Student;
The keyword typedef defines the entire structure type struct student as an alias Student, which means that struct student is also called Student. With this alias, you can use "alias variable name" to define variables of type struct student. For example, Student stu; A structure type variable named stu is defined, which is the same as struct student stu; The effect is exactly the same.
You can also use the keyword typedef to specify the same structure type as a different data type alias, such as an alias in the form of a pointer. For example,
typedef struct student { long int num; char name[20]; char sex; char addr[20]; }Student,*stuPtr;
Here, two aliases are specified. Student is the alias of struct student structure type, and stuPtr is the alias of pointer type pointing to struct student structure type.
stuPtr sPtr; Equivalent to Student *sPtr; Or struct student *sPtr;.
one ️⃣✨✨✨ Structure variable initialization and reference ✨✨✨
After the structure type is defined, there are three methods to initialize the structure type variable.
(1) After the structure type is defined, define the variable immediately and assign the initial value in the way of {}.
struct student { long int num; char name[20]; char sex; char addr[20]; } stu={202018080,"qingyun",'m',"xiaoxian"};
(2) After defining a structure type, when defining variables of this type in other places, initialize them in the way of {}.
struct student stu={202018080,"qingyun",'m',"xiaoxian"};
(3) You can specify a member variable name for initialization.
struct student stu={.num=18, .name="qingyun"};
Reference of structure type variable (common)
The reference structure member variable is a little different from the reference basic data type variable. In order to access the structure member, use the member selector (.), The member selector is a dot between the structure variable name and the structure member to be accessed. For example, there is a definition of struct student stu. To assign values to two member variables in the structure, you can use the following two statements.
stu.num=202018080; strcpy(stu.name, "qingyun");stu.num=202018080; strcpy(stu.name, "qingyun");
Comprehensive example
Given a structure type, define two variables stu1 and stu2 of this structure type, initialize stu1, assign the value of stu1 to stu2, modify the two member values of stu2, and finally output all member values of the two variables.
#include <stdio.h> typedef struct student { long int num; char name[20]; char sex; char addr[20]; }Student; int main(void) { /*Define two variable structure variables stu1 and stu2, and initialize stu1 */ Student stu1={202018080,"qingyun",'m',"xiaoxian"},stu2; stu2=stu1; //Assign the value of stu1 to stu2 to assign the values of all member variables. stu2.num=202018081; //Assign a value to the num member variable of stu2. strcpy(stu2.name, "wenchu"); //Assign a value to the name member variable of stu2. /*The following is the value of the output structure. Note that you should assign it to the basic type.*/ printf("no.:%hd name:%s sex:%c address:%s\n",stu1.num,stu1.name, \ stu1.sex, stu1.addr); printf("no.:%hd name:%s sex:%c address:%s\n",stu2.num,stu2.name, \ stu2.sex, stu2.addr); return 0; }
two ️⃣✨✨✨ Structure array ✨✨✨
typedef struct student { long int num; char name[20]; char sex; char addr[20]; }Student;
Now you can use it to define an array, such as defining a one-dimensional array Student stu[5];. There are five elements in this array, each of which is of type Student. Similarly, the value of the array name stu is a pointer value pointing to the 0th element of the array (Student stu[5]; equivalent to struct student {stu[5];).
Like the basic type one-dimensional array, an array of structure types can be initialized with a sequential subscript initializer, a specified initializer, and a mixture of the two. For example,
Student stu[5]={{1,"zhang",'M',"Anhui"},{2,"wang",'M',"Henan"}}; Student stu[5]={[1]={1,"zhang",'M',"Anhui"},{2,"wang",'M',"Henan"}};
After defining an array, you can use the method of "array variable. Member variable" to reference the member variable of one of its elements. For example, to access the num member variable of the 0th element of the array, it can be written as stu [0] num.
For example, stu1 and stu2 are initialized with structure array
#include <stdio.h> typedef struct student { int num; char name[10]; char sex; char addr[10]; }Student; int main(void) { /*Define two variable structure variables stu1 and stu2, and initialize stu1 */ Student stu[2]={{202018080,"qingyun",'m',"xiaoxian"}, \ {202018081,"wenzhu",'w',"xiaoxian"}}; printf("no.:%d name:%s sex:%c address:%s\n",stu[0].num,stu[0].name, \ stu[0].sex, stu[0].addr); printf("no.:%d name:%s sex:%c address:%s\n",stu[1].num,stu[1].name, \ stu[1].sex, stu[1].addr); return 0; }
three ️⃣✨✨✨ Pointer to structure type ✨✨✨
Student *p;
Here p is an address variable, accounting for 4 or 8 bytes in the general compiler. The space pointed to by p stores data of Student type.
When referring to the member variable of the structure, the pointer variable pointing to the structure type is referenced with "-", for example, p - > num; Represents a num member variable in the structure type data pointed to by reference p.
#include <stdio.h> typedef struct student { long num; char name[20]; char sex; float score; }Student,*Ptr; int main(void) { Student stu={202018080,"qingyun",'m',97.5}; Ptr p; p=&stu; printf("no.:%ld,name:%s,sex:%c,score:%f\n",stu.num,stu.name,stu.sex,stu.score); printf("no.:%ld,name:%s,sex:%c,score:%f\n", (*p).num,(*p).name,(*p).sex,(*p).score); printf("no.:%ld,name:%s,sex:%c,score:%f\n", p->num,p->name,p->sex,p->score); return 0; }
four ️⃣✨✨✨ Structure type data as function parameters ✨✨✨
When calling a function, the argument of the structure type copies the values of all its member variables to the formal parameter variables.
Example 10-5-1 in the main function, define a structure variable, input its member value, and then define a function to output all member values of the structure type variable.
#include <stdio.h> typedef struct student { long num; char name[10]; char sex; float score; }Student; void print(Student stu) //Defines the formal parameter of the structure type and receives the value of the argument variable. { printf("no.:%ld name:%s sex:%c score:%-5.1f\n", \ stu.num,stu.name,stu.sex,stu.score); } int main(void) { Student stu1={202018080,"qingyun",97.5}; print(stu1); return 0; }