In the last lesson, we learned the definition and initialization of struct. When we define a student, we 'd better give the name to char *, and the student ID should also use char *, we have limitations in using int to indicate student IDs, because they cannot represent numbers with characters! Because some student numbers are not necessarily pure numbers! What about the score! Use the float type and zip code! Mailing address! Char! Because some mailing addresses are long, long, and short! Haha! Does it feel like it is a string of previous knowledge points! We used to learn char!
Structures cannot be defined recursively.
Let's look at this example:
Struct person {char name [16]; char sex; int age; float height; struct person per ;}
As you can see, we cannot calculate the size of the struct! With regard to recursion, I am calling myself for the moment, and then there must be an exit. Otherwise, recursion will continue like this (seemingly infinite recursion, however, the computer memory stack should be limited. At some point, it may overflow. I don't know if it is like this. I will try to correct it if something wrong ). The biggest advantage of recursion is good readability! In the future, I will study recursion.
Struct type variable
After we define the struct type, we only describe the composition of this type and do not allocate memory space to it. When we define the struct variable, it will allocate memory space for it, just as int cannot allocate memory space, and int a has memory space!
Now let's take a look at how to define struct variables. In fact, I have learned this morning. Let's review it again:
Struct person {char name [64]; char sex; int age; float height ;}; struct person one;
Then the struct variables are arranged in the memory and we learned this morning. Then I will review the sequence of variables defined by us. The size of the struct is 64 + 1 + 2 + 4. (The alignment mode here is mod 1 ). if the alignment mode is address Mode 4, its size will be different! Be sure to pay attention to it!
Another definition of struct Variables
We define variables while defining types:
In fact, the two definitions have the same effect. Hey! I almost forgot! There are also 3rd definition methods:
Struct person
{
Char name [64];
Char sex;
Int age;
Float height;} one;
Struct {
Char name [64];
Char sex;
Int age;
Float height;
}
One;
In this way! Directly define the struct type variable, removing the type name! Haha! Remember! This definition form is different from the previous two methods, because the name of this struct type is not described, therefore, this method is usually used without the need to define this type of struct variable again. This is like marriage in some countries, such as the Philippines. Divorce is not allowed!
Struct variable reference
References to struct variables must be made after struct variables are defined. References to struct variables can be divided into references to Members in the body variables and references to the entire struct variables. Generally, the operations on struct variables are performed in the unit of members.
The reference format is: struct variable name. member name
The. Priority is the highest priority in the operator. Remember it! When a member is a struct variable, the member operator must be used continuously! Until the basic data type. Each member of a struct variable belongs to a certain data type, so you can perform various operations permitted by its type like a common variable.
Let's look at an example:
// What about this program! In our analysis, a struct person is defined in the main function, and the struct person is nested to define a struct date. We will analyze one row and one row.
# Include <stdio. h> void main (){
Struct person;
// Defines a struct.
{
Char name [20];
// The first member is the name, a 20-byte array.
Char sex; // defines the gender.
Struct date // nesting defines a struct of the date type and defines three members.
{
Int year;
Int month;
Int day;
}
Birthday;
// Defines the date struct variable birthday. float height;
// Defines the fload height.
}
Per;
// Defines the person struct type variable per. printf ("Enter the name :");
Gets (per. name );
Per. sex = M;
Per. birthday. year = 1982;
Per. birthday. year ++;
Per. birthday. month = 12;
Per. birthday. day = 15;
Per. height = (175 + 176)/2;
Printf ("% s % 3c % 4d % 2d % d % 7.1f", per. name, per. sex, per. birthday. month, per. birthday. day, per. birthday. year, per. height );
}
Precautions for operating the struct
Let's look at the following program:
# Include <stdio. h>
Struct stDate {
Int nYear;
Int nMonth;
Int nDay;
};
Struct stStudent {
Char szName [64];
Struct stDate WoW;
Float fScore [3];
};
Int main (){
Struct stStudent stu;
Stu. szName = "dodolook ";
Return 0;
}
Is this program correct ?! As a matter of fact, you can see an error. Why! The reason is that stu. szName is an array name and a constant, so an error occurs! How can we assign values to character arrays! We can do this by using a function called strcpy. How to write it! Strcpy (stu. szName, "dodolook"); this is correct.
References to the entire struct variable
You can assign values to struct variables of the same type. Struct variables can only be assigned as a whole. Other operations such as input and output must reference members of struct variables.
For example, stu2 = stu; it is to copy the values of stu members one by one to stu2. this is equivalent
Memcpy, memory copy. Let's take a look at the memcpy help documentation on how to copy memory in the near future:
Void * memcpy (void * dest, const void * src, size_t count );
The first parameter is the destination address, the second parameter is the source address, and the third parameter is the length of the data to be copied, in bytes. Size_t is actually an int. This function returns a void pointer. We have never seen this type before! Let's take a closer look at how one pointer carries two pieces of information, one is the address information, and the other is the explanation of the address. So void indicates there is no information, so we won't be able to get the content of the void type pointer! No information is also a kind of information, just like no style is also a kind of style, haha!
Why is void used here ?!
Because the person who writes this function does not know what type of data you want to copy.
Instance: memcpy (& stu2, & stu, sizeof (stu ));
Struct Array
After we define a struct type, it is equivalent to another available variable type. In fact, float is also a struct, which is only measured in bits. Let's take a look at an example of a struct array:
Struct person {char name [20]; char sex; int age; float height ;}; struct person per [3];
Haha! Is it really fun! Now our array element has a struct. Haha!
What is per here! Per is a pointer constant of the person type! In fact, this is almost the same as the array!
Let's look at a subprogram:
# Include <stdio. h>
Struct stDate
{
Int nYear;
Int nMonth;
Int nDay;
};
Struct stStudent {
Char szName [64];
Char Sex;
Struct stDate WoW;
Float fScore [3];
};
Int main ()
{
Struct stStudent stu;
Printf ("% d", (int) stu. fScore-(int) & stu );
Return 0 ;}
Note: The default mode is 4.
How much is printed in this example ?! Let's count together, 64 + 4 + 12 = 80, right! Haha. In this example, can we find the offset from the member of the struct variable to the first address of the struct! Right! Great! But there is a disadvantage here. We must use struct variables. Can we find the offset of one member without a struct variable ?! This problem will be explained in tomorrow's notes.