Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368974
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-21 17:05:14

  •  what is difference enum and union
Enumerations (enum)
Enumerations create new data types to contain something different that is not limited to the values fundamental data types may take. Its form is the following:

enum enumeration_name {
 value1,
 value2,
 value3,
 .
 .
} object_names;


For example, we could create a new type of variable called colors_t to store colors with the following declaration:

enum colors_t {black, blue, green, cyan, red, purple, yellow, white};


Notice that we do not include any fundamental data type in the declaration. To say it somehow, we have created a whole new data type from scratch without basing it on any other existing type. The possible values that variables of this new type color_t may take are the new constant values included within braces. For example, once the colors_t enumeration is declared the following expressions will be valid:

1
2
3
4
colors_t mycolor;

mycolor = blue;
if (mycolor == green) mycolor = red;


Enumerations are type compatible with numeric variables, so their constants are always assigned an integer numerical value internally. If it is not specified, the integer value equivalent to the first possible value is equivalent to 0 and the following ones follow a +1 progression. Thus, in our data type colors_t that we have defined above, black would be equivalent to 0, blue would be equivalent to 1, green to 2, and so on.

We can explicitly specify an integer value for any of the constant values that our enumerated type can take. If the constant value that follows it is not given an integer value, it is automatically assumed the same value as the previous one plus one. For example:

1
2
3
enum months_t { january=1, february, march, april, may, june, july, august, september, october, november, december} y2k;


In this case, variable y2k of enumerated type months_t can contain any of the 12 possible values that go from january to december and that are equivalent to values between 1 and 12 (not between 0 and 11, since we have made january equal to 1).

Unions
Unions allow one same portion of memory to be accessed as different data types, since all of them are in fact the same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally different:

union union_name {
 member_type1 member_name1;
 member_type2 member_name2;
 member_type3 member_name3;
 .
 .
} object_names;


All the elements of the union declaration occupy the same physical space in memory. Its size is the one of the greatest element of the declaration. For example:

1
2
3
4
5
union mytypes_t {
 char c;
 int i;
 float f;
 } mytypes;


defines three elements:

1
2
3
mytypes.c
mytypes.i
mytypes.f


each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent of each other.

  1. #include <stdio.h>

  2. enum colors_t
  3. {
  4.         black,
  5.         blue,
  6.         green,
  7.         red,
  8.         yellow,
  9.         white
  10. };

  11. union mytypes_t
  12. {
  13.         char c;
  14.         int i;
  15. };

  16. int
  17. main(void)
  18. {
  19.         int i;
  20.         enum colors_t ct;
  21.         for (i = 0; i < 6; i++) {
  22.                 ct = i;
  23.                 switch (ct) {
  24.                         case black:
  25.                                 printf("black\n");
  26.                                 break;
  27.                         case blue:
  28.                                 printf("blue\n");
  29.                                 break;
  30.                         case green:
  31.                                 printf("green\n");
  32.                                 break;
  33.                         case red:
  34.                                 printf("red\n");
  35.                                 break;
  36.                         case yellow:
  37.                                 printf("yellow\n");
  38.                                 break;
  39.                         case white:
  40.                                 printf("white\n");
  41.                                 break;
  42.                         default:
  43.                                 break;
  44.                 }
  45.         }


  46.         union mytypes_t u;
  47.         u.c = 'A';
  48.         printf("%c\n", u.c);
  49.         u.i = 5;
  50.         printf("%d\n", u.i);

  51.         return (0);
  52. }
Result:
  1. black
  2. blue
  3. green
  4. red
  5. yellow
  6. white
  7. A
  8. 5

阅读(1194) | 评论(1) | 转发(0) |
0

上一篇:calloc 和 malloc

下一篇:虚函数

给主人留下些什么吧!~~

onezeroone2011-04-24 16:23:53

enum---created a whole new data type from scratch without basing it on any other existing type。
union---All the elements of the union declaration occupy the same physical space in memory。