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

What is the common body of C language?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the common body of C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the C language common body?"

By Phil Newell / Edward

Common body, also known as federation, is a special data type in C language. It allows users to store different data types in the same memory location. Users can define a common body with multiple members, but these members share the same piece of memory at any time. For example, the storage of the common body is like space sand played by children. A piece of space sand is fixed in size, and there are only so many, while the storage inside the shared body is like one mold after another. You can divide this piece of space sand into two smaller bear molds, and it becomes two bears, or you can put it in a larger tiger mold. It becomes a tiger. Therefore, the essence of the common body is different forms of access to the same block of memory. To define the common body, you must use the union statement in a manner similar to defining the structure. The union statement defines a new data type with multiple members. The format of the union statement is as follows: union common body label (default) {data type member variable 1; data type member variable 2;... ;} common volume variable 1, common volume variable 2. We can find that, in fact, the form of the common body is the same as the structure, but the key words are not the same. When defining, the common body tag can be omitted, and the member variable data type within the union is a standard variable definition, such as int I; or float f; or other valid variable definition, of course, it can also be a structure. Let's define a common body type called Data, which has three members I, f, and str, defined as follows: union Data {int ibot float f str [20];} data;, the common body of this Data type can now store an integer, a floating point number, or a string. This means that a variable (the same memory location) can store multiple types of data. You can use any built-in or user-defined data type in a common body as needed. The memory of the Data common body is shown in figure 1. Fig. 1 the memory occupied by the shared storage body should be sufficient to store the largest member of the shared body. For example, in the above example, Data will take up 20 bytes of memory because the string takes up the largest amount of space among the members. The member variable access inside the common body is the same as the structure body, using the "common body. Member name" is fine. The following example will show the total memory occupied by the above common body. The idea of programming is that we define a common body, store variables of different lengths in it, and finally use sizeof to measure the common body and look at the output of the final program. The specific code is shown in figure 2. Figure 2 measures whether the common memory is accessed in different forms of the same memory space in the final analysis. You can access this memory as int, or you can access it as char array. We can illustrate the above argument with an example. The process of argument is simple. Let's first define a common body in which two variables are defined, one of type "unsignedint" and one of type array of type "char". Then assign values to the variables of unsigned int, and finally print out both variables. As shown in figure 3.

Fig. 3 access to different forms of the same block of memory as we said before, when an array is defined, its internal element address increases upward, so after we assign a value to the array in the community, we use a four-byte unsigned integer variable to access it. You can get the combined values. In general, computers in small-end mode (high bytes are stored at high addresses) put the array elements of high addresses in the high bytes of 4-byte unsigned integers, and the array elements of low addresses on the low bytes of 4-byte unsigned integers. The big-end mode (high-byte storage to low address) is the opposite. Using this principle, we can judge the size of computer storage. At this point, I believe you have a deeper understanding of "what is the C language common body". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report