TextEnumerated, Structure and Union Types
Objectives:
At the end of this topic, you will be able to
- Define the enumerated type
- Define the typedef (type definition)
- Discuss unions and bit-fields
- Define a structure
- Access structure members and perform operations on individual members
- Discuss array of structures
- Discuss array within a structure
- Discuss structures and functions
- Discuss pointers to structures
- Discuss self referential and complex structures
- Understand the operations on structures
- Discuss command line arguments
Enumerated Type
The enumerated type is a user defined type based on the standard
integer type.
In an enumerated type, each integer value is given an identifier
called an enumeration constant.
We can thus use the enumerated constants as symbolic names, which
make our programs much more readable.
Declaration of
enumeration:
enum<enum_name> {member1,member2, ...... .... ....};
‘enum’ is the keyword, which is followed by an identifier and a set of
enumeration constants enclosed in a set of braces.
The statement is terminated with a semicolon. By default compiler
assigns the first identifier the value zero, the second identifiers value as
one and so on.
If we assign the value explicitly for the identifier, the next
identifiers value will be incremented by one. That is if the identifiers value
is 4 the next identifiers value will be 5.
Note: for enumeration identifiers we use uppercase alphabetic characters.
Example: enum option {YES,
NO, CANCEL};
by default YES has value 0, NO has value 1 and CANCEL has 2.
Example: enum direction {
EAST = 1, SOUTH, WEST = 6, NORTH};
Now EAST has the value 1, SOUTH has the value 2 and WEST has the
value 6 and now the NORTH value will be 7.
Example: enum color {RED,
BLUE, GREEN, WHITE};
Operations on Enumerated
Types:
Assigning values to enumerated types:
After an enumerated variable has been declared, we can store values
in it. These can hold only values for the declared type.
Example:
Defining a color variable and using it in several statements.
enum color x;
enum color y;
enum color z;
x=BLUE;
y=WHITE;
z=PURPLE; //Error. There is no purple.
Similarly, once a variable has been defined and assigned a value, we
can store its value in another variable of the same type.
x=y;
z=y;
Comparing Enumerated
Types:
Enumerated types can be compared using the comparison operators
Example:
Given two color variables, we can compare them for equal, greater
than, or less than. We can also compare them to enumeration constants.
if (color1 ==
color2
------------------------
if (color1 ==
BLUE)
------------------------
Another natural use of enumerated types is with the switch
statement. As enumerated types are derived from integer types, they may be used
in the case expressions.
Enumeration Type
Conversion:
Enumerated types can be converted implicitly or cast explicitly. The
compiler implicitly casts enumerated type variables/constants to an integer as
required. When we implicitly cast an integer to an enumerated type we get
either a warning or an error.
Observe the example of
implicit casts:
int x;
enum color y;
x=BLUE; // valid. X contains 1
y=2; // compiler warning
We can explicitly cast an integer to an enumerated type without
problems. To assign ‘y’ as the value BLUE in the above code, we need to write
the following code.
enum color y;
y= (enum color 2); //Valid.
y contains BLUE
Typedef
‘typedef’ is a keyword, which allows the user to specify a new name
for a data type which is already defined in the C language program.
We can use the typedef statement to re-define one of C’s standard
types (int, float, etc..).
typedef identifier is coded in the uppercase as shown.
Typedef type IDENTIFIER;
Example of using typedef
To declare an
array of pointers to strings:
Old method:
char * pStrArray[20];
we can simplify the declaration by using a type definition to create
a string type and then defining the array using the new type.
New method:
typedef char*STRING;
STRING pStrArray1[20];
STRING pStrArray1[35];
By using the typedef, we can create a new data type. The keyword
“typedef” is to be used while defining the new data type/.
Syntax:
typedef type data
name;
here, type is the data type and data name is the user defined name
for that type.
typedef int
hours;
hers, hours is another name for int and now we can use hours instead
of int in the program as follows.
hours hrs;