Union usage in C Language

Source: Internet
Author: User

Union usage in C Language
Union shared declaration and definition of a shared variable:

"Union" is a special class and a data structure of the construction type. Different data types can be defined in a "Union,

In a variable described as the "union" type, any data defined by the "union" can be loaded, which shares the same memory segment,

To save space (there is also a space-saving type: Bit domain ). This is a very special feature and also a feature of union.

In addition, like struct, Union default access permissions are also public and also have member functions.

 

 

 


A shared object (refer to the "shared object" Encyclopedia entry) is a special form of variable that uses the keyword union to define the shared body (some people are also called "union ") the Declaration is very similar to the definition of the shared body variable and the struct. The format is:

Union shared body name {data type member name;...} variable name;

The shared body indicates that several variables share the same memory location. Different data types and variables of different lengths are stored at different times.
In union, all the common body members share one space,
In addition, only one member variable value can be stored at a time.

1. Multiple members can be defined in union. The size of union is determined by the maximum size of members.
2. union Members share the memory of the same block. Only one member can be used at a time.
3. assigning values to a member will overwrite the values of other members (because they share a piece of memory.

However, the premise is that the number of bytes occupied by members is the same. When the number of bytes occupied by members is not the same, only the values of the corresponding bytes will be overwritten,

For example, assigning values to char members does not overwrite the entire int member,

Because char occupies only one byte, and int occupies four bytes

4. The order in which union members are stored starts from the low address.

For example, the following code:

// Shared body C language // Yang Xin # include
 
  
# Include
  
   
Typedef union {char c; int a; int B;} Demo; int main () {Demo d; d. c = 'H'; d. a = 10; d. B = 12; printf ("the length of the variable at the beginning is % d \ n", sizeof (d)/4 ); printf ("the three values after the assignment are \ n"); printf ("% c \ t % d \ n", d. c, d. a, d. b); return 0 ;}
  
 





 

 

 

Related Article

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.