Union usage in C language

Source: Internet
Author: User

Union Common Declaration and common one variable definition:

Union is a special class and a data structure of a constructed type. You can define a number of different data types within a "union".

A variable that is described as a type of "union" that allows any kind of data defined by the "union" to be loaded, which shares the same memory,

To achieve space-saving purposes (there is also a space-saving type: bit field). This is a very special place and a characteristic of union.

In addition, as with structs, federated default access is public and has member functions as well.




Shared body(refer to "Common Body" encyclopedia entry) is a special form of variable that uses the keyword union to defineThe shared body (some people are also called "union") statements andA common body variable definition is very similar to a struct. The form is:

Union common body Name {data type member name; data type member name; ...} variable name;

A common body means that several variables share a single memory location, saving different data types and variables of varying lengths at different times.
In union, all members of the common body share a space,
and can only store them at the same time.the value of a member variable.

1. Multiple members can be defined in union, and the size of the Union is determined by the size of the largest member.
2. Union members share memory of the same block size and only one member can be used at a time.
3. Assigning a value to a member overrides the values of the other members (because they share a piece of memory.)

But only if the member occupies the same number of bytes, when the member's number of bytes does not only overwrite the value on the corresponding byte,

For example, assigning a value to a char member does not overwrite the entire int member,

Because Char occupies only one byte, and int accounts for four bytes

4. The order in which the Union union is stored is that all the members are stored from the low address.

For example, the following code:

Shared body C language//Xin Yang # include <stdio.h> #include <stdlib.h>typedef union{char c;int a;int b;} Demo;int Main () {Demo d;d.c = ' H ';d. A = 10;D.B = 12;printf ("The byte length at the beginning of the variable is:%d\n", sizeof (d)/4);p rintf ("Three values after assignment: \ n"); printf ("%c\t%d\t%d\n", D.C, D.A, d.b); return 0;}







Union usages in the

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.