C Language Learning course seventh chapter-Structure and Union (8)

Source: Internet
Author: User
Tags definition integer printf

I. DEFINITION of a joint

The general form of defining a union type is:
Union Union name
{
Member table
};
A member table contains several members, and the general form of a member is that the name of the type descriptor member name should conform to the specification of the identifier.
For example:
Union Perdata
{
int class;
Char office[10];
};
Defines a union type named Perdata that contains two members, one integer, the member is class, and the other is an array of characters, named Office. After a joint definition, a joint variable description is described as a variable of type perdata that can hold an integer class or a character array office.

Ii. description of the joint variable

The description of the union variable is the same as the description of the structure variable, and there are three forms. Define, explain, define, explain and direct. Take the Perdata type as an example, as follows:
Union Perdata
{
int class;
Char officae[10];
};
Union Perdata A,b; /* Description A,b for perdata type * *
Or it can be described as:
Union Perdata
{int class;
Char office[10]; }a,b; or directly stated as: union
{int class;
Char office[10]; }a,b
The A,B variable after the description is Perdata type. Their memory allocation diagram is shown in Figure 7-8. The length of the a,b variable should be equal to the longest length in the member of the Perdata, equal to
The length of the office array, a total of 10 bytes. As you can see from the diagram, the a,b variable, when given an integer value, uses only 2 bytes, and 10 bytes when given an array of characters.

Assignment and use of Union variables

The assignment to a union variable can only be performed on a member of a variable. A member of a union variable is represented as a union variable name. Member name for example, after a is described as a variable of type Perdata, you can use the A.class a.office to not allow assignment of only union variable masterpieces or other operations. Initialization assignments for union variables are also not allowed, and assignments can only be made in the program. It is also emphasized that a union variable can be assigned only one member value at a time. In other words, the value of a union variable is the value of a member of the Union change.
[Example 7.15] there is a common form for teachers and students, the teacher data are name, age, occupation, teaching and learning four. Students have name, age, occupation, class four.
Program input personnel data, and then output in tabular form.
Main ()
{
struct
{
Char name[10];
int age;
char job;
Union
{
int class;
Char office[10];
} Depa;
}BODY[2];
int n,i;
for (i=0;i<2;i++)
{
printf ("Input name,age,job and department\n");
scanf ("%s%d%c", body[i].name,&body[i].age,&body[i].job);
if (body[i].job== ' s ')
scanf ("%d", &body[i].depa.class);
Else
scanf ("%s", Body[i].depa.office);
}
printf ("Name\tage job class/office\n");
for (i=0;i<2;i++)
{
if (body[i].job== ' s ')
printf ("%s\t%3d%3c%d\n", Body[i].name,body[i].age
, Body[i].job,body[i].depa.class);
Else
printf ("%s\t%3d%3c%s\n", Body[i].name,body[i].age,
Body[i].job,body[i].depa.office);
}
}
This example program uses a structure array body to store personnel data, which has a total of four members. Where the member item DEPA is a union type, the union is composed of two members, one for the integer class, and one for the character array office. In the first for statement of the program, enter the individual data for the person, enter the first three member Name,age and job of the structure, and then identify the job member, such as "s" for the Union Depa class input (the class number for the student) or else the DEPA office input ( The teacher assigned to the teaching and education).

When entering with a scanf statement, be aware that a member of an array type, whether it is a struct member or a federated member, cannot be added to the "&" operator before the item. As in line 18th of the program
Body[i].name is an array type, Body[i].depa.office is also an array type in line 22nd, so you cannot add the "&" operator between the two items. The second for statement in the program is used to output values for each member item:

Summary of this chapter

1. Structure and union are two types of constructed data, which is an important means for users to define new data types. There are many similarities between structure and union, and they are all made up of members. Members can have different data types. The representation method for the member is the same. Can be described as variable in three different ways.

2. In the structure, each member occupies its own memory space, and they are present at the same time. The total length of a struct variable equals the sum of all member lengths. In a union, all members cannot occupy its memory space at the same time, and they cannot exist at the same time. The length of the union variable equals the length of the longest member.

3. "." is a member operator that can be used to represent member items, and members can also be represented by the "->" operator.

4. The structure variable can be used as a function parameter, and the function can also return pointer variables to the structure. A union variable cannot be a function argument, nor can a function return a pointer variable that points to a union. However, you can use a pointer to a union variable, or you can use a federated array.

5. The structure definition allows nesting, and the structure can also be used as a member to form a structure and a joint nesting.

6. The linked list is an important data structure, which facilitates the dynamic storage allocation. This chapter is a one-way linked list, but also can be composed of two-way linked lists, circular chain list, and so on.

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.