This article brings a detailed explanation of enumeration and union related knowledge!
If you think the article is good, I look forward to your one-click three-link. Your encouragement is the source of motivation for my creation. Let us work together, run together, and let us meet at the top! ! !
Table of contents
2. Definition of enumeration type
2. The memory layout and characteristics of the union
1. Enumeration
1. What is an enumeration?
2. Definition of enumeration type
for example:
in:
verify:
You can also assign an initial value when declaring an enumeration type
like:
verify:
When some enumeration constants are assigned, the size of other values
for example:
verify:
3. Advantages of enumeration
4. Use of enumeration
2. Consortium (community)
1. Definition of joint type
union Un is a union type;
n is a union variable created with this type;
2. The memory layout and characteristics of the union
analyze:
feature:
Because the characteristic of the union is that these members share the same space, but if you want to be able to store these union variables, if you only open up the size of the char type, then the value of a cannot be stored. Therefore, the size of such a joint variable is at least the size of the largest member (because the union must at least have the ability to save the largest member)
Note: Only one can be used at a time for members of the union;
An example of a union feature code:
Example interview questions:
Determine the size and endian storage of the current computer
Reference code and analysis:
union Un
{
int a;
char c;
};
int main()
{
union Un n;
n.a = 1;
if (n.c == 1)
{
printf("小端存储\n");
}
else
{
printf("大端存储\n");
}
return 0;
}
analyze:
operation result:
3. Calculation of joint size
The size of the union is at least the size of the largest member
analyze:
verify:
end of this chapter~