`union` in C
union
in C
union
in C
1. union
union
are declared and used in the same way as structures. Like structures, it is also used to store different types of data.union
的声明和使用与结构体相同。像结构体一样,它也用于存储不同类型的数据。
Before going into the differences between structure
and union
, let’s first see how to define a union
.
在探讨 structure
and union
之间的区别之前,我们首先来看看如何定义 union
。
2. Defining a Union
Let’s look at the syntax of a union.
union union_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
};
Let’s take the same example we took in structure. Here we will take the name of union
as student
and name
, roll_no
and phone_number
as its members as follows.
让我们举一个与结构体相同的例子。在这里,我们将 union
的名称命名为 student
,name
, roll_no
and phone_number
作为其成员,如下所示。
union student
{
int roll_no;
char name[30];
int phone_number;
};
3. Declaration of Union Variable
We declare the variables of a union
in the same way as we declare those of a structure.
我们声明 union
变量的方式与声明结构体变量的方式相同。
Again, let’s take the same example in which we want to store the roll no
, name
and phone number
of three students. To do this, we will define a union
named student
as we have defined above and then we will declare its three variables p1
, p2
and p3
(which will represent the three students respectively) in the main function.
同样,让我们以相同的示例为例,我们要存储三个学生的 roll no
, name
and phone number
。为此,在主函数中我们将定义一个上面定义的名为 student
的 union
,然后在其中声明其三个变量 p1
, p2
and p3
(分别代表三个学生)。
union student
{
int roll_no;
char name[30];
int phone_number;
};
main()
{
union student p1, p2, p3;
}
There is another method of declaring union
variables where we declare these at the time of defining the union
as follows.
还有另一种声明联合变量的方法,我们在定义联合时按如下方式声明它们。
union student
{
int roll_no;
char name[30];
int phone_number;
} p1, p2, p3;
Like structure, we assign the name
, roll no
and phone number
of the first student (suppose p1
) by accessing its name
, roll_no
and phone number
as follows.
与结构体类似,我们通过如下方式分配第一个学生的 name
, roll no
and phone number
(假设为 p1
),方法是访问其学生的 name
, roll_no
and phone number
。
p1.roll_no = 1;
Here, p1.roll_no
implies roll_no
of p1
.
imply [ɪmˈplaɪ]:vt. 意味,暗示,隐含
Always use strcpy
to assign a string value to a variable as we assigned the name "Brown"
to p1
(first student).
始终使用 strcpy
为变量分配字符串值,因为我们将名称 "Brown"
分配给了 p1
(第一个学生)。
strcpy(p1.name, "Brown");
Till now, you must have found union
similar to structure. Now let’s look at the differences between the two.
到现在为止,您一定已经发现与结构体相似的联合。现在让我们看一下两者之间的区别。
4. Difference between union and structure
Before going into the differences, let’s first look at an example.
//============================================================================
// Name : union
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
struct student1
{
int roll_no;
char name[40];
int phone_number;
};
union student2
{
int roll_no;
char name[40];
int phone_number;
};
int main()
{
struct student1 s1;
union student2 u1;
printf("size of structure = %d\n", sizeof(s1));
printf("size of union = %d\n", sizeof(u1));
return 0;
}
Output
size of structure = 48
size of union = 40
Talking about the above example, the amount of memory required to store a structure is the sum of the memory sizes of all its members. In the above example, the memory sizes of the variables roll_no
and phone_number
will be 4 bytes each (since both are of type integer) and the memory size of the character array name[40]
will be 40 bytes (since the array occupies the memory of 40 characters and the size of char is 1). Thus, the memory occupied by the structure will be 4 + 40 + 4 = 48 bytes
.
谈论上面的示例,存储结构体所需的内存量是其所有成员的内存大小的总和。在上面的示例中,变量 roll_no
和 phone_number
的内存大小将分别为 4 个字节 (因为它们都是整数类型),而字符数组 name[40]
的内存大小将为 40 个字节 (因为数组占用 40 个字符的内存,并且 char 的大小为 1)。因此,该结构占用的内存为 4 + 40 + 4 = 48 bytes
。
Now coming to the union
, the memory size of a union
is equal to the size of its member occupying the maximum space in the memory. The size of roll_no
and phone_number
is 4 bytes each and that of name[40]
is 40 bytes. So, the union
will occupy a memory space of 40 bytes.
现在进入 union
,union
的内存大小等于其成员占用内存最大空间的大小。roll_no
and phone_number
的大小分别为 4 个字节,而 name[40]
的大小为 40 个字节。因此,union
将占用 40 个字节的存储空间。
4.1 依照 int 字节数对齐
//============================================================================
// Name : union
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
struct student1
{
int roll_no;
char name[39];
int phone_number;
};
union student2
{
int roll_no;
char name[39];
int phone_number;
};
int main()
{
struct student1 s1;
union student2 u1;
printf("size of structure = %d\n", sizeof(s1));
printf("size of union = %d\n", sizeof(u1));
return 0;
}
size of structure = 48
size of union = 40
4.2 依照 int 字节数对齐
//============================================================================
// Name : union
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
struct student1
{
int roll_no;
char name[38];
int phone_number;
};
union student2
{
int roll_no;
char name[38];
int phone_number;
};
int main()
{
struct student1 s1;
union student2 u1;
printf("size of structure = %d\n", sizeof(s1));
printf("size of union = %d\n", sizeof(u1));
return 0;
}
size of structure = 48
size of union = 40
4.3 依照 int 字节数对齐
//============================================================================
// Name : union
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
struct student1
{
int roll_no;
char name[37];
int phone_number;
};
union student2
{
int roll_no;
char name[37];
int phone_number;
};
int main()
{
struct student1 s1;
union student2 u1;
printf("size of structure = %d\n", sizeof(s1));
printf("size of union = %d\n", sizeof(u1));
return 0;
}
size of structure = 48
size of union = 40
4.4 依照 int 字节数对齐
//============================================================================
// Name : union
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
struct student1
{
int roll_no;
char name[36];
int phone_number;
};
union student2
{
int roll_no;
char name[36];
int phone_number;
};
int main()
{
struct student1 s1;
union student2 u1;
printf("size of structure = %d\n", sizeof(s1));
printf("size of union = %d\n", sizeof(u1));
return 0;
}
size of structure = 44
size of union = 36
We can access only one member of union
at a time because we have only one location in memory for it, so only one of the member can be used at a time. All the other members will contain the garbage value (i.e. will get corrupted). This is not the case with structures where we can access all the members variables at the same time because diiferent memory spaces are occupied by each.
我们一次只能访问 union
的一个成员,因为我们在内存中只有一个位置,因此一次只能使用一个成员。所有其他成员将包含垃圾值 (即会被破坏)。对于可以同时访问所有成员变量的结构体而言,情况并非如此,因为每个成员空间占用不同的存储空间。
corrupt [kəˈrʌpt];adj. 腐败的,贪污的,堕落的 vt. 使腐烂,使堕落,使恶化 vi. 堕落,腐化,腐烂
Now let’s see an example of union
.
//============================================================================
// Name : union
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
union student
{
int roll_no;
int phone_number;
char name[30];
};
int main()
{
union student p1;
p1.roll_no = 1;
p1.phone_number = 1234567822;
strcpy(p1.name, "Brown");
printf("roll_no : %d\n", p1.roll_no);
printf("phone_number : %d\n", p1.phone_number);
printf("name : %s\n", p1.name);
return 0;
}
Output
roll_no : 2003792450
phone_number : 2003792450
name : Brown
As you can see, the name
got printed as it is and garbage value got printed as roll number
and phone number
. Let’s see why this happened.
如您所见,name
被原样打印,roll number
and phone number
被打印为垃圾值。让我们看看为什么会这样。
As you now know that we can access only one member of union
at a time and the other members get corrupted. So, when we wrote p1.roll_no = 1
, member roll_no
got accessed and got assigned a value a value 1
. After that, by writing p1.phone_number = 1234567822
, we assigned a value to the member phone_number
. Finally, we assigned a string value "Brown"
to the member name
as strcpy(p1.name,"Brown")
and now, the other two members roll_no
and phone_number
contains a garbage value. Thus, the value of name
got printed as it is and the values of the other two variables in the output are the garbage values of those two.
如您现在所知,我们一次只能访问 union
的一个成员,而其他成员则被损坏。因此,当我们编写 p1.roll_no = 1
时,成员 roll_no
被访问并被赋予一个值 1
。之后,通过写入 p1.phone_number = 1234567822
,我们为成员 phone_number
分配了一个值。最后,我们将一个字符串值 "Brown"
分配给成员 name
,使用 strcpy(p1.name, "Brown")
,现在,其他两个成员 roll_no
and phone_number
包含一个垃圾值。这样,name
的值就照原样打印了,输出中其他两个变量的值就是这两个变量的垃圾值。
Let’s see one more example.
//============================================================================
// Name : union
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <string.h>
union student
{
int roll_no;
char name[30];
int phone_number;
};
int main()
{
union student p1;
p1.roll_no = 1;
printf("roll_no : %d\n", p1.roll_no);
strcpy(p1.name, "Brown");
printf("name : %s\n", p1.name);
p1.phone_number = 1234567822;
printf("phone_number : %d\n", p1.phone_number);
return 0;
}
Output
roll_no : 1
name : Brown
phone_number : 1234567822
Here, first we assigned the value of roll_no
as 1 and printed it. So currently, the value of the other members is some garbage value and that of roll_no
is 1, so it got printed as it is. After that, we assigned a string value "Brown"
to name
, so now roll_no
and phone_number
contained garbage values and name
got printed. Similarly, we printed phone_number
.
在这里,首先我们将 roll_no
的值分配为 1 并打印出来。因此,当前其他成员的值是一些垃圾值,而 roll_no
的值是 1,因此按原样打印。此后,我们为 name
分配了一个字符串值 "Brown"
,因此现在 roll_no
and phone_number
包含了垃圾值并打印了名称。同样,我们打印了 phone_number
。
References
上一篇: jsp中复选框选中的删除