结构体struct和联合体union以及enum枚举体5的区别
下面来自wikipedia:
In computer science, a union is a value that may have any of several representations or formats; or it is a data structure that consists of a variable which may hold such a value. Some programming languages support special data types, called union types, to describe such values and variables. In other words, a union type definition will specify which of a number of permitted primitive types may be stored in its instances, e.g., "float or long integer". Contrast with arecord (or structure), which could be defined to contain a float and an integer; in a union, there is only one value at any given time.
A union can be pictured as a chunk of memory that is used to store variables of different data types. Once a new value is assigned to a field, the existing data is overwritten with the new data. The memory area storing the value has no intrinsic type (other than just bytes or words of memory), but the value can be treated as one of several abstract data types, having the type of the value that was last written to the memory area.
In type theory, a union has a sum type.
Depending on the language and type, a union value may be used in some operations, such as assignment and comparison for equality, without knowing its specific type. Other operations may require that knowledge, either by some external information, or by the use of a tagged union.
http://en.wikipedia.org/wiki/Union_type
Difference between Union and Structure
A union is a class all of whose data members are mapped to the same address within its object. The size of an object of a union is, therefore, the size of its largest data member.
In a structure, all of its data members are stored in contiguous memory locations. The size of an object of a struct is, therefore, the size of the sum of all its data members.
This gain in space efficiency, while valuable in certain circumstances, comes at a great cost of safety: the program logic must ensure that it only reads the field most recently written along all possible execution paths. The exception is when unions are used for type conversion: in this case, a certain field is written and the subsequently read field is deliberately different.
An example illustrating this point is:
+-----+-----+
struct { int a; float b } gives | a | b |
+-----+-----+
^ ^
| |
memory location: 150 154
|
ˇ
+-----+
union { int a; float b } gives | a |
| b |
+-----+
Structures are used where an "object" is composed of other objects, like a point object consisting of two integers, those being the x and y coordinates:
typedef struct {
int x; // x and y are separate
int y;
} tPoint;
Unions are typically used in situation where an object can be one of many things but only one at a time, such as a type-less storage system:
typedef enum { STR, INT } tType;
typedef struct {
tType typ; // typ is separate.
union {
int ival; // ival and sval occupy same memory.
char *sval;
}
} tVal;
更多参考:http://www.chinaunix.net/old_jh/23/179471.html
1.由于结构体每个成员都有自己的存储空间,所有所有成员可以同时存储,而共用体的所有成员都共享一个存储空间,同一时间只能存储一个成员。
2.结构体变量可以在定义时初始化,而共用体变量不能在定义时初始化。
如下面的程序是非法的:
union Person{
int age;
double salary;
}u={,,};
3.结构体变量中的成员可以同时存储,可以通过指针或变量单独引用每个成员;而共用体同一时间只能存储一个成员,可以通过指针或变量单独引用所存储的成员,并且所引用的是最后一次存入成员的值,例如;
union Person{
int age;
double salary;
}u;
int main()
{
u.age=;
u.salary=1.1;
cout<<u.salary<<endl;
}
输出:1.1
如果我们输出u.age,会发现是一个很乱的数字。
我们把age和salary都改成int,如下:
union Person{
int age;
int salary;
}u;
int main()
{
u.age=-;
u.salary=;
cout<<u.salary<<endl;
cout<<u.age<<endl;
}
输出:都输出1,。证明了age和salary共享内存。
Ansi c 允许2个同类型的共有体变量之间赋值,和结构体类似。
共用体变量的应用
他可以增加程序的灵活性,对同一段内存空间的值在不同情况在不同情况下做不同的用途。至少有2方面的用途。
1.应用在数据处理中
如在一个学校的人员管理中,对教师应登记其“单位”,对学生应登记其“班级”,他们都在同一栏中。
struct {
long num;
char name[];
char sex;
char job;
union
{
int className;//班级
char group[];//单位名
}category;
}person[];
int main()
{
cin>>person[].job;
if(person[].job=='s')//s代表Student
cin>>person[].category.className;
else if(person[].job=='t')//t代表teacher
cin>>person[].category.group;
//cout
if(person[].job=='s')
cout<<person[].category.className<<endl;
else if(person[].job=='t')
cout<<person[].category.group<<endl;
}
2.发现数据的底层存储形式。
利用共用体的特点区分整形变量的高字节和低字节。
union change
{
char c[];
short int i;
}un;
int main()
{ un.i=;
printf("%d ",un.c[]);
printf("%d ",un.c[]); }
输出:1,0。
结构体最常用的就是大小端的判断:
bool isSmall()
{
union{
char c;
int x;
}un;
un.x=; if(un.c==)
cout<<"小端"<<endl;
else
cout<<"大端"<<endl;
return un.c==;
}
小端:低位低地址,高位高地址。
大端:低位高地址,高位低地址。
Intel cpu 都是小端存储。
enum 所谓枚举类型,是指这种类型的变量的值只能是所指定的若干名字之一。例如一个枚举类型和枚举变量可以定义成:
enum colorName{ red,yellow,blue,white,black}; (C++struct可以省略struct,而enum不能省略,即不能这么定义变量:
colorName c1,而必须是:
enum colorName c1,c2;
变量c1是colorName类型,其值只能是red,yellow,blue,white,black 五者之一。
赋值如:
c1=red;或c1=white。
而下面的语句不合法
c1=green;
需要说明的几点:
1.enum是关键字,标识枚举类型,定义枚举必须用enum开头。
2.定义enum时,花括号中的一些名字(red,yellow等)都是程序设计者自己指定的,命名规则和标识符相同。这些名字没有固定含义,只是一个符号。程序设计者为了可读性才使用这些名字。这些名字不是变量,不能改变其值,例如:
下面的不对: red=3,scanf("%d",red);
3.它的值是一些整数,从花括号中的第一个名字开始,各名字分别代表0,1,2,3,4。这是系统自动赋予的。如:
printf("%d",red) 输出的值为0.
但是定义枚举时不能写成:
enum ColorName {0,1,2,3,4};
必须用符号red,yellow或其他标志符,这些符号称为枚举常量或枚举元素。
4.可以在定义类型时对枚举常量初始化。
enum ColorName{red=3,yellow,blue,white=8,black};
这是red值为3,yellow为4,blue为5,white为8.black为9.
5.枚举常量可以进行比较:
if(c1==red) printf("red");
if(c1>red)
if(c1<white).
他们是按照所代表的整数进行比较的。
6.一个枚举变量的值只能是几个枚举 常量之一。可以将一个枚举变量赋给另一个枚举变量。但不能讲一个整数赋值给他。
c1=black;正确
c1=5;//错误
7.枚举常量不是字符串,不是用下面的方法输出字符串red:
printf("%s",red);
可以先检查color的值,如果是red,就输出字符串red。
enum string
{
x1=5,
x2,
x3,
x4,
};
enum string x=x3;
此时,枚举变量x3实际上是7。
常用枚举
enum Weekday {sunday,monday,tuesday,wednesday,thursday,friday,saturday};
遍历枚举:
好多资料都是这样说的;
enum ColorName{ red,yellow,blue,white,black};
int main()
{
enum ColorName c1;
for(c1=red;c1<black;c1=c1+)
{
switch(c1)
{
case red:
cout<<"red";
break;
}
}
}
我编译错误:
rror C2440: “=”: 无法从“int”转换为“ColorName。
为什么?因为c1+1变成了1,(枚举和整形数据相运算)是整形,而整形不能赋值给枚举类型。
结构体struct和联合体union以及enum枚举体5的区别的更多相关文章
- C和C++中结构体(struct)、联合体(union)、枚举(enum)的区别
C++对C语言的结构.联合.枚举 这3种数据类型进行了扩展. 1.C++定义的结构名.联合名.枚举名 都是 类型名,可以直接用于变量的声明或定义.即在C++中定义变量时不必在结构名.联合名.枚举名 前 ...
- 结构体struct、联合体union、枚举类型enum
1.c语言中的类型 1)内置类型——char,short,int,float,double: 2)用户自定义类型(UDT)——struct结构体,union联合体,enum枚举类型 2.内存对齐 2. ...
- 结构体struct 与 联合union
1.C语言中的结构体 1.1 定义 结构体是由一系列相同或不同类型的变量组成的集合. struct 结构体名{ //struct为关键字,“结构体名”为用户定义的类型标识. ...
- 联合体union和大小端(big-endian、little-endian)
1.联合体union的基本特性——和struct的同与不同 union,中文名“联合体.共用体”,在某种程度上类似结构体struct的一种数据结构,共用体(union)和结构体(struct)同样可以 ...
- c++ anonymous union,struct -- 匿名联合体和机构体
c++ anonymous union,struct -- 匿名联合体和机构体 结构体和联合体各自的基本用法不赘述,仅说一下他们匿名时访问的情况.如果是token不同,可以直接跨层访问.例子 #inc ...
- 1.0 基础、标示符、常量、数据类型(enum 枚举,struct 结构体)、操作符、循环、数组
一.程序 现实生活中,程序是指完成某些事务的一种既定方法和过程,可以把程序看成是一系列动作执行过程的描述. 在计算机世界,程序是指令,即为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集 ...
- struct、union、enum and sizeof
struct 1.结构体和数组的差别:能够再结构体里声明数组.结构体变量能够相互赋值.而数组不行. 2.struct与class的差别:class的成员訪问权限默认是private,而struct成员 ...
- <struct、union、enum>差异
关于C++和C的区别 区别最大的是struct,C++中的struct几乎和class一样了,可以有成员函数,而C中的struct只能包含成员变量. enum,union没区别. struct的定义 ...
- Swift中元组(Tuples),结构体(Struct),枚举(Enums)之间的区别
Swift有许多种存储数据方式,你可以用枚举(enums),元组(tuples),结构体(structs),类(classes),在这篇文章中我们将比较枚举.元组.结构体之间区别,首先从最简单的开始- ...
随机推荐
- Android Animation学习(一) Property Animation介绍
Android Animation Android framework提供了两种动画系统: property animation (introduced in Android 3.0)和view an ...
- CSS文字样式
font-family:通常文章的正文使用的是易读性较强的serif字体,用户长时间阅读下不easy疲劳.而标题和表格则採用较醒目的sans-serif字体.Web设计及浏览器设置中也推荐遵循此原则. ...
- log4net日志的配置及简单应用
在程序运行中,往往会出现各种出乎开发人员意料的异常或者错误,所以,记录详细的程序运行日志信息,有利于开发人员和运维人员排查异常信息,提高工作效率.而本菜鸟在大神推荐和指导下使用log4net这一插件工 ...
- 深入解读JavaScript面向对象编程实践
面向对象编程是用抽象方式创建基于现实世界模型的一种编程模式,主要包括模块化.多态.和封装几种技术.对JavaScript而言,其核心是支持面向对象的,同时它也提供了强大灵活的基于原型的面向对象编程能力 ...
- Git跨平台中文乱码临时解决方案
Git 是一个非常优秀的分布式版本控制系统,最初为Linux Kernel版本管理进行量身定做.优点是,和其他版本控制系统相比,稳定,速度快,跨平台,易学易用,无需要花费成本.更多优点请点击阅读:ht ...
- 支持iOS9 Universal links遇到的问题
记录为iOS9上的APP支持Universal links遇到的一些问题. 在Web服务器上传apple-app-site-association文件 必须支持HTTPS获取配置文件 文件名后不加.j ...
- WPF datagrid 如何隔行变色
<DataGrid AlternationCount="2"> <DataGrid.RowStyle> <Style TargetType=" ...
- 笔记 postgresql oid同步
以前学习postgresql的笔记 create table 消耗 OID 如create table my_test_table, 他本身会消耗一个 会在pg_type中插入两条记录_my_test ...
- BEGIN_SINK_MAP(CMainDlg) SINK_ENTRY(IDC_EXPLORER1, ..。响应不到的
</pre><pre name="code" class="cpp"> class CMainDlg : public CAxDialo ...
- [LeetCode]题解(python):097-Interleaving String
题目来源: https://leetcode.com/problems/interleaving-string/ 题意分析: 给定字符串s1,s2,s3,判断s3是否由s1和s2穿插组成.如“abc” ...