Common use of C language

Source: Internet
Author: User

1: Definition of the common Body (Union) type

Union myunion{int A;char b;};     int main (void) {myunion u1;u1.a = 23;printf ("u1.b =%d\n", u1.b);  23printf ("&u1.a =%p \ n",& (u1.a));  &u1.a = 0xbfbe5c04printf ("&u1.b =%p \ n",& (u1.b)); &u1.b = 0XBFBE5C04}

In general, the definitions of common and structural bodies are similar, and the methods used are basically the same

2: The difference between a common body and a struct

(1) The structure is similar to a parcel, the members of the structure are independent of each other, distributed in the memory of the different units, they are just packaged into a whole is called the structure; the members of the common body are all one and are not independent, they use the same memory unit. Can be understood as: Sometimes this element, sometimes it is that element. It is more accurate to say that the same memory space is interpreted in many ways. that is, the common body is the same memory space, the stored value (binary) is the same, using u1.a to access the memory is the type of int to parse the memory space, using u1.b to access the memory is the char type to parse the memory space. In general, a shared body is a different way of understanding the same piece of memory space in which the binary number is stored.

(2) When using sizeof to measure the size of a common body, the size of the element that occupies the largest amount of memory in each element of the Union is measured. Because of this, we can store all the elements.

(3) There is no memory alignment problem with the elements in the Union , because there are actually only 1 types of memory space in the Union , starting with the same address (the start address is the first address of the memory space the entire Union occupies), Therefore, memory alignment is not involved.

3: Main use of the common body

(1) A common body is used in a situation where many different rules of the same memory unit are parsed.

(2) In the C language, there can be no common body, with pointers and coercion type conversion can replace the common body to accomplish the same function, but the way of sharing the body is simpler, more convenient, better understanding.










Common use of C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.