The definition and application of common body are analyzed in detail

  • 2020-04-02 01:26:37
  • OfStack

Definition:
Using the overwrite technique, several variables overwrite each other, so that several different variables occupy the same memory structure, become a community type structure.

The definition of a community is similar to a structure, except that all members of the community are stored in the same segment of memory, starting at the same address, and only one of the member variables can be used at a time.

The general form of a declaration common is:


union  Common type names 
{
     Member list 
};

The general form of defining a common variable is:
Common type names     Shared variable names;
Such as:

union data{
int        i;
char   ch;
double d; 
};
data a,b,c;

or

union{
int        i;
char   ch;
double d; 
};
data a,b,c;

The length of a structure variable is the sum of the memory length of each member. Each member occupies its own memory unit. The memory length of a Shared variable is equal to the length of the longest member.

Common use
1. You cannot apply a Shared variable, but only refer to a member of a Shared variable.

Such as:


cout<<a.i;
cout<<a.ch;

2. The purpose of using common variables is to hold several different types of data in a single memory segment.

Note, however, that only one can be stored at a time, not in a storage set. Also, if the new member variable is used, the value of the original member variable is overwritten.


#include<iostream>
using namespace std;
int main(){
   union data{
    char a;
    char b;
   };
   data qianshou;
   qianshou.a='q';
   cout<<qianshou.a<<endl;
   qianshou.b='m';
   cout<<qianshou.a<<endl;
   return 0;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201308/201308311103103.jpg ">

Later, we assigned member b in the pool, and we printed member a and the value of b, so they turned out to be public addresses.

3. Cannot assign a value to a Shared variable name, and cannot attempt to reference the variable name to get a value; You cannot initialize a Shared variable when you define it, and you cannot use the name of a Shared variable as a function parameter.

An example of a community:
There are a number of personnel data, including students and teachers. Student data include: name, number, sex, occupation, grade. Teacher data include: name, number, sex, occupation, and title. It can be seen that students and teachers contain different data. They were first asked to put them in the same form:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201308/201308311103104.jpg ">

Requires the designer to input personnel information and then output it.

If everyone is regarded as a structural variable, it can be seen that the first four member variables of teachers and students are the same, and the fifth member variable may be class or position. When the fourth member variable is s, the fifth member variable is class. When the fourth member variable is t, the fifth member variable is position.


#include<iostream>
#include<string>
using namespace std;
int main(){
   struct{
      string name;
      string num;
      char   sex;
      char   job;
      union{
       char grade[5];
       char position[5];
      }p;//Defines a community variable
    }person[2];//Defines an array of struct variables
   int i=0;
   cout<<"nametnumtsextjobtclass/position"<<endl;
   for(;i<2;i++){
 cin>>person[i].name>>person[i].num>>person[i].sex>>person[i].job;
 if(person[i].job=='s')cin>>person[i].p.grade;
 else cin>>person[i].p.position;    
   }
   i=0;
   cout<<"===========show data==========="<<endl;
   for(;i<2;i++){
    cout<<person[i].name<<"t";
    cout<<person[i].num<<"t";
    cout<<person[i].sex<<"t";
    cout<<person[i].job<<"t";
    if(person[i].job=='s')cout<<person[i].p.grade<<endl;
 else cout<<person[i].p.position<<endl;            
   }
   cout<<endl;
   return 0;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201308/201308311103105.jpg ">


Related articles: