Meaning of union in C Language

Source: Internet
Author: User

Union is a data type, which is similar to struct in the form:

Union union Union name {

Type1
Name1;

Type2
Name2;

......

} Name of the federated variable;

The Union indicates that several variables share a memory location, saving different data types and different data lengths at different times. Compile when a union is describedProgramAutomatically generates a variable whose length is the maximum variable length in the Union. The access method and structure of the federated members are the same.

Difference between struct and union:

1. The structure and union are composed of multiple members of different data types, but only one selected member is stored in the Union at any time, and all members of the structure exist.

2. Assignment of different members of the Union will be rewritten to other members. The original member value does not exist, but the assignment of different members of the structure will not be affected.

You can only assign values to members of a federated variable. A federated variable can only be assigned to one member at a time. The value of a federated variable is a member value of the current federated variable.

It should be noted that the so-called sharing here does not mean that multiple members are loaded into a joint variable at the same time, but that the joint variable can be assigned to any member value, however, only one value can be assigned at a time. If a new value is assigned, the old value is washed away.


Http://blog.csdn.net/jiangnanyouzi/article/details/3158702

  1. BoolIsbigendian ()
  2. {
  3. Union
  4. {
  5. IntI;
  6. CharC;
  7. } Test;
  8. Test. c = 2;
  9.  
  10. ReturnTest. I! = 2;
  11. }

Here, union is used to control the shared layout,The knowledge point is that the Members C and I in the Union are aligned from the low address..

Http://visionsky.blog.51cto.com/733317/151760

About anonymous union:

The anonymous Union member name cannot conflict with other variable names in the same action.

Because the vertex operator is not used for anonymous union, the elements contained in the anonymous union must be data and member functions are not allowed,

  It cannot contain private or protected members, and the global anonymous union must be static. Otherwise, it must be placed in the anonymous namespace.

The members in the Union share the memory, so static and reference (because the memory cannot be shared) cannot be used.

Classes with constructors, destructor, and copy operators cannot be stored in the consortium. Because they share the memory, the compiler cannot guarantee that these objects will not be destroyed, you cannot call the Destructor when you exit.

, Your sister's, it's still not clear!

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.