Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use union in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use union in C language". In daily operation, I believe many people have doubts about how to use union in C language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use union in C language". Next, please follow the editor to study!

Common body union

What is a Commons union? It is estimated that this common body is relatively rare in the code, so I went to see it. In fact, this common body union (also known as a union) is very similar to the definition of the structure, for example: typed.

It is very similar in meaning, variable definition and usage. Combine many types, as in the following two examples

Union st {char a; int b;}

A common body is also a custom type that you can use to create variables, such as:

Union num {int n; char ch; double f;}; union num a, b, c

You can also create variables while defining the common body:

Union num {int n; char ch; double f;} a, b, c

If you do not define a new variable, you can also omit the name of the Commons:

Union {int n; char ch; double f;} a, b, c; boil deconstruction

At this point, I'll talk about the structure, which is used to store a set of different types of data. The definition of a structure is as follows:

Struct structure name {

A variable or array contained in a structure.

}

Is there any origin between structure and common body?

Here is a code to start with:

# include typedef union {int a; char c; / / int a; / / int b;} st; int main (void) {st bit; bit. Clockwise Bamboo; / / bit. Await 10; / / bit. Baked 60; printf ("the size is%d\ n", sizeof (bit)); printf (". Centering% d\ n", bit. C); return 0;}

We can see the results:

From the above code, we can see that the size of the common body is not the sum of the memory occupied by each member as we did in the previous structure, but as we said above, the common body is determined by the one with the largest amount of memory occupied by the members.

In the above example, int occupies 4 bytes, which is the largest, so the result of sizeof (bit) is 4 bytes. In fact, the printed result an and b are 60, which is the value of the member with the largest access memory footprint, because the ASCII code value of that'B'is 66. Union accesses its members in the same way as a structure

In fact, the structure is similar to a human body, the organs of the human body are independent of each other, and so are the members of the structure, distributed in different units of memory, they are just packaged into a whole called the structure; the members of the common body are actually one, not independent of each other, and they use the same memory unit. Can be understood as: sometimes it is this element, sometimes it is that element. It is more accurate to say that there are many ways to interpret the same memory space.

At this point, the study of "how to use union in C language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report