Structural body
In Java, we want to indicate that a composite data type will be encapsulated using objects. and C has a structure.
A struct is a custom data type in C that is a set of variables, different from arrays, and arrays are limited to the same data type, and structs can be any data type, including arrays. Every variable or array inside a struct is a member of a struct.
Here's a look at the basic use of structs
14-18 lines, defines a structure called person, the structure of which has age,money,sex three data types;
20-24 lines, is the structure of the declaration of a person named P a variable, and assign value;
25-27 lines, is the basic use of the structure, it can be seen that the structure of the value of the time and Java is very similar, are in the middle. To take the value of one of the variables in the form.
Here 's a look at the use of struct pointers.
29-30 lines, defines a struct pointer, called PI, and assigns the address of the struct variable p to pi.
32 line, the structure of the use of pointers, since PI is the structure of p pointer, then, take the structure of that is the front add a * slightly, so simple, divert, now take the structure of P, then to p inside the simple, directly using the way in front of the middle add a. To
33 line, is the pointer structure of another way of use, direct use, you can take the corresponding value, than the 32 line is not a lot of false orders?
Length of the structure body
The structure and structure of the pointer are all finished, and finally look at the length of the structure body.
The length is simple, directly put inside the accumulation is not, int and float are accounted for 4 bytes, and char accounted for 1 bytes, then directly add up is 9.
In the end is not, verify a bit know.
Look, the printed result is 12, gee, why so Strange, does char also account for 4 bytes?
Yes, that's it, char also accounts for 4 bytes, why? Because the other two variables in the length are 4, then the char inside is also defined as 4 bytes, so that the pointer can be easily offset.
But this is not absolute, the same code, the result of execution is not the same, this and the compiler and the environment is not the same, haha.
It is important to note that, although all are 4 bytes, the front also said that this is to facilitate the offset of the pointer, then naturally think of the pointer to the addition and subtraction operation, then we use the line?
If I'm going to take its money field, then I'm like: printf ("%f", * (p+1)); Can you do that? No, you can try it!
Finally, three types of structure are attached:
The code inside the comment is also relatively clear, here is no longer said, the structure of the introduction to this end, see the Consortium
Complex:
The structure is understood, the Union is simple.
A consortium is similar to a struct, and the key word for a struct is a struct and the Union is the key . Look at the code:
This seems to be no different from the structure. No difference, there will be no consortium. So look at the following code:
After the above code, the first line of the print result is 12, this does not have too much explanation, and then when you go to the Union of money to assign value, you can print the value of age, you will find that the value of age is completely wrong, and you go to print money value, you will find money The value is the right one.
So, this is the difference between the two, the structure of the data will be constantly covered, and the structure of the data is independent of each other.
Now the question is, what is the length of the consortium? Or is it cumulative? It doesn't make sense to accumulate so much memory that so little data is worth the candle, so the length of the union is the length of the longest variable in the union.
The introduction of the consortium is here, too.
Both the struct and the consortium are introduced, and finally, an enumeration is introduced, which means that the enumeration is exhaustive, that is, only one of the values in this one, not the other.
Look at the code:
14-17 rows are declarations of enum types;
The 19 rows are defined and assigned values;
20 line is the result of printing, why is the result of printing int type value? And why is it 1?
This is simple, the enumeration is exhaustive, then is limited, and not infinite, since it is limited, then the enumeration of each value will be numbered, like an array, starting from 0 numbering, which is why the printed value is an int, why is it 1? Because the Monday in the enumeration is the 2nd, so simple.
Of course, if you don't like numbering starting from 0, you can also customize an initial value:
As you can see from the code, the enumeration can not only customize the starting value itself, it can also be customized in the middle, and the following will be followed by +1
Enumeration, just get here first.
C language-structure-union