结构体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),在这篇文章中我们将比较枚举.元组.结构体之间区别,首先从最简单的开始- ...
随机推荐
- Java程序栈信息文件中的秘密(五)
最近发现在使用jstack工具在导出Java应用的线程栈时有一个小小的窍门,比如Linux环境上有一个用户为appuser,假如以这个用户启动了一个Java进程B,如果想要导出进程B的线程栈,则必须切 ...
- c/c++处理参数
直接上代码:涉及函数getopt(),getopt_long() #include <unistd.h> #include <stdlib.h> #include <st ...
- 关于js封装框架类库之DOM操作模块(二)
上一篇基本实现了框架结构,但是与真正能用上的项目框架比较还是存在很多不足,在这又做了些加强与优化 (function ( window, undefined ) { var arr = [], pus ...
- eclipse或adt-bundle创建的android项目没有自动生成MainActivity.java和activity_main.xml等文件解决办法
以前我电脑一直以来都是用的eclipse3.7来开发android项目的,创建android项目也能正常生成MainActivity.java和activity_main.xml等文件.后来不知道什么 ...
- string s = HttpContext.Current.Server.MapPath("");
string s = HttpContext.Current.Server.MapPath(""); 获取当前文件夹路径 而后用相对路径读取图片
- Unable to locate appropriate constructor on class异常
一般出现Unable to locate appropriate constructor on class这个异常,都是实体类的带参数的构造方法和使用查询语句出现偏差,两个地方的代码如下: 一般都是第 ...
- MFC 简单实现 DES 算法
前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...
- (iOS)推送常见问题
1.为什么启动的时候出现 Did Fail To Register For Remote Notifications With Error的错误程序运行的时候出现下面的错误信息: did Fail T ...
- (IOS)关于Xcode的架构(Architectures)设置
首先来了解一下Architectures中几个参数的含义 ARMv6:ARM11内核用于iPhone2G和iPhone3G中的架构 ARMv7:modern ARM内核用于iPhone3GS和iPho ...
- photoshop自动切图
自动切图 前面的话 随着photoshop版本的不断升级,软件本身增加了很多新的功能,也为切图工作增加了很多的便利.photoshop最新的版本新增了自动切图功能,本文将详细介绍photoshop的这 ...