Definition and application of common body detailed analysis of _c language

Source: Internet
Author: User

Defined:
Using overlay technology, several variables cover each other so that several different variables share the same memory structure and become the community type structure.

The definition of a community is similar to a struct, but all members of the community are stored in the same memory, the starting address, and only one member variable can be used at the same time.

The general form of declaring a common body is:

Copy Code code as follows:

Union Common body Type name
{
Member List
};

The general form of the definition of a common body variable is:
Common body type name common body variable name;
For example:
Copy Code code as follows:

Union data{
int i;
Char ch;
Double D;
};
Data a,b,c;

or
Copy Code code as follows:

union{
int i;
Char ch;
Double D;
};
Data a,b,c;

The length of the structure variable is the sum of the memory length of each member. Each member occupies its own unit of memory. The total memory length of the shared-body variable equals the length of the longest member.

Use of the common body
1. You cannot apply a shared-body variable, but only a member of a shared-body variable.

For example:

Copy Code code as follows:

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

2. The purpose of using a common-body variable is to store several different types of data through a unified memory segment.

Note, however, that you can only store one, not a set, at a single instant. Also, if the new member variable is used, the value of the original member variable is overwritten.

Copy Code code as follows:

#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;
}



Later, we assigned the member B of the shared body, and we output the member A and the value of B, so that they are public addresses.

3. You cannot assign a value to a shared-body variable name, you cannot attempt to refer to a variable name to get a value, you cannot initialize it when you define a shared-body variable, and you cannot use a shared-body variable name as a function parameter.

An example of a community:
Data on a number of persons, including students and teachers, are provided. Student data include: name, number, gender, occupation, grade. The teacher's data include: name, number, gender, occupation, position. It can be seen that students and teachers contain different data. First ask them to be placed in the same table:



The design program is required to enter personnel information and then output.

If you think of everyone as a structural variable, you can see that the first 4 member variables of the teacher and student are the same, and the fifth member variable may be class or position, and 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.

Copy Code code as follows:

#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 a structural body array variable
int i=0;
cout<< "Name\tnum\tsex\tjob\tclass/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;
}


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.