下面来自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的区别的更多相关文章

  1. C和C++中结构体(struct)、联合体(union)、枚举(enum)的区别

    C++对C语言的结构.联合.枚举 这3种数据类型进行了扩展. 1.C++定义的结构名.联合名.枚举名 都是 类型名,可以直接用于变量的声明或定义.即在C++中定义变量时不必在结构名.联合名.枚举名 前 ...

  2. 结构体struct、联合体union、枚举类型enum

    1.c语言中的类型 1)内置类型——char,short,int,float,double: 2)用户自定义类型(UDT)——struct结构体,union联合体,enum枚举类型 2.内存对齐 2. ...

  3. 结构体struct 与 联合union

    1.C语言中的结构体 1.1 定义 结构体是由一系列相同或不同类型的变量组成的集合. struct 结构体名{               //struct为关键字,“结构体名”为用户定义的类型标识. ...

  4. 联合体union和大小端(big-endian、little-endian)

    1.联合体union的基本特性——和struct的同与不同 union,中文名“联合体.共用体”,在某种程度上类似结构体struct的一种数据结构,共用体(union)和结构体(struct)同样可以 ...

  5. c++ anonymous union,struct -- 匿名联合体和机构体

    c++ anonymous union,struct -- 匿名联合体和机构体 结构体和联合体各自的基本用法不赘述,仅说一下他们匿名时访问的情况.如果是token不同,可以直接跨层访问.例子 #inc ...

  6. 1.0 基础、标示符、常量、数据类型(enum 枚举,struct 结构体)、操作符、循环、数组

    一.程序 现实生活中,程序是指完成某些事务的一种既定方法和过程,可以把程序看成是一系列动作执行过程的描述. 在计算机世界,程序是指令,即为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集 ...

  7. struct、union、enum and sizeof

    struct 1.结构体和数组的差别:能够再结构体里声明数组.结构体变量能够相互赋值.而数组不行. 2.struct与class的差别:class的成员訪问权限默认是private,而struct成员 ...

  8. <struct、union、enum>差异

    关于C++和C的区别 区别最大的是struct,C++中的struct几乎和class一样了,可以有成员函数,而C中的struct只能包含成员变量. enum,union没区别. struct的定义 ...

  9. Swift中元组(Tuples),结构体(Struct),枚举(Enums)之间的区别

    Swift有许多种存储数据方式,你可以用枚举(enums),元组(tuples),结构体(structs),类(classes),在这篇文章中我们将比较枚举.元组.结构体之间区别,首先从最简单的开始- ...

随机推荐

  1. 用上Google才是正事 分享几个訪问Google的IP和域名

    通过VPN或者GAE等代理进行訪问,GAE下载请移步<GAE 3.1.18 最新版本号下载 用上Google才是正事>.这是大家通经常使用的办法.也有同学们不愿意使用代理软件.那今天来分享 ...

  2. 实习生的Django[1]

    尽管学期尚未结束,暑假尚未到来,可是大三的同学非常多已经和我一样開始实习或者实习一段时间了.我仅仅面试了一间数据挖掘的公司的研发部,还算顺利通过. 来这里实习后,由于网络原因,昨天没有刷题也没有写BL ...

  3. c++之构造函数学习

    #include<stdio.h> class Test {      private:      int i;      int j;      int k;     public :  ...

  4. UVa1585 Score

    #include <stdio.h> int main(){    int T, O, score;    char str[81], *p;    scanf("%d" ...

  5. BZOJ 1211: [HNOI2004]树的计数( 组合数学 )

    知道prufer序列就能写...就是求个可重集的排列...先判掉奇怪的情况, 然后答案是(N-2)!/π(d[i]-1)! -------------------------------------- ...

  6. JSP——九大内置对象和其四大作用域

    一.JSP九大内置对象: JSP根据Servlet API 规范提供了某些内置对象,开发者不用事先声明就可以使用标准的变量来访问这些对象. Request:代表的是来自客户端的请求,例如我们在FORM ...

  7. Could not load type System.ServiceModel.Activation.HttpModule解决办法

    等注册完成后网站就可以打开了. win2008下提示未能从程序集“System.ServiceModel, Version=3.0.0.0问题解决 在Windows Server 2008中的IIS服 ...

  8. 利用python进行数据分析之绘图和可视化

    matplotlib API入门 使用matplotlib的办法最常用的方式是pylab的ipython,pylab模式还会向ipython引入一大堆模块和函数提供一种更接近与matlab的界面,ma ...

  9. python 10min系列之实现增删改查系统

    woniu-cmdb 奇技淫巧--写配置文件生成增删改查系统 视频教程 项目主页跪求github给个star, 线上demo,此页面都是一个配置文件自动生成的 详细的文章介绍和实现原理分析会发布在我的 ...

  10. poj 3304 计算几何

    大意: 是否存在一条直线,使所有线段在直线上的投影至少交与一点 思路: 转换为是否存在一条直线与所有的线段相交,做这条直线的垂线,那么垂线即为所求 **/ #include <iostream& ...