Additional C Types Enumerations and Unions enum 1








![Structure Approach struct catalog_item{ int stock_number; float price; int item_type; char title[100]; char author[100]; Structure Approach struct catalog_item{ int stock_number; float price; int item_type; char title[100]; char author[100];](https://slidetodoc.com/presentation_image_h2/75be56ddabc64e319d60eaff9b5d23b0/image-9.jpg)

![Use Unions to Save Memory (2) int j; struct item x[5]; x[0]. item_type = Use Unions to Save Memory (2) int j; struct item x[5]; x[0]. item_type =](https://slidetodoc.com/presentation_image_h2/75be56ddabc64e319d60eaff9b5d23b0/image-11.jpg)

- Slides: 12
Additional C Types: Enumerations and Unions
enum (1) u u enum is useful to define several consecutive constant numbers instead of using multiple #defines. In addition, enum also defines a data type. To define a enum type: enum days{ Sun, Mon, Tue, Wed, Thu, Fri, Sat; }; u To declare a variable with the days type: enum days one_day; u To assign a value to the variable: one_day = Mon ; u To define a new type to avoid having to write the “enum” part of enum days: typedef enum days_type;
enum (2) u In C, an enum variable actually represents an integer. The above code is almost equivalent to: const int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int one_day=Mon; u u u We use the word almost because the storage size of one_day might be different. The use of enum increases the readability of your code. It is possible to assign a value not in the range to an enum variable: enum days my_rest_day; my_rest_day = -1; /* this is allowed, by why? */
enum (3) u If you do not want it start from zero, you can use: enum week_days{ Mon=1, Tue, Wed, Thu, Fri }; u This is identical to: const int Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6;
Unions u An union, like a structure, consists of one or more data members. u The difference is that those data members in a union actually use the same memory space.
Structures vs. Unions (1) struct number{ char ch; int i; } x; union number{ char ch; int i; } y; ch ch i i
Structures vs. Unions (2) x. i = 100; x. ch = 'a'; printf("%c, %d", x. ch, x. i); Outputs: a, 100 Outputs: a, 1627390052 0 x 61000064
Why Do We Need Unions? u Suppose there are three types of merchandise: books, mugs, and shirts. – Books: Title, author, number of pages – Mugs: Design – Shirts: Design, color, size u We want to store all the items in an array. So, we must use the same data type to describe all three different kinds of merchandise.
Structure Approach struct catalog_item{ int stock_number; float price; int item_type; char title[100]; char author[100]; int num_pages; char design[100]; int color; int size; } int j; struct catalog_item x[5]; x[0]. item_type = 0; strcpy(x[0]. title, "unix"); …… for(j = 0; j<5; j++){ switch(x[j]. item_type){ 0: puts(x[j]. title); break; 1: puts(x[j]. design); break; 2: …… } }
Use Unions to Save Memory (1) struct item{ int stock_number; float price; int item_type; union{ struct{ char title[100]; char author[100]; int num_pages; } book; struct{ char design[100]; } mug; struct{ int size; } shirt; } item; };
Use Unions to Save Memory (2) int j; struct item x[5]; x[0]. item_type = 0; This is a perfect place to use an enum to avoid these constants and do strcpy(x[0]. book. title, "unix"); something more meaningful! …… for(j = 0; j<5; j++){ switch(x[j]. item_type){ 0: puts(x[j]. book. title); break; 1: puts(x[j]. mug. design); break; 2: …… } }
Classes in Java and C++ vs. Unions u Unions are not as necessary in Java and C++ as the ability to declare classes and derive subclasses can do many of the same things unions can. class item{ int stock_number; float price; } class book extends item{ String title; …… } ……