Practical C Programming chapter 12 advance types
C provides a rich set of data types, including structures, unions, and enumerated types.
1. Structures
In a strcuture, each element or field is named and has its own data type.
The general form of a structure definition is
The general form of a structure variable is
struct structure-name variable-name;
To access field in a structure, use "." operator:
variable.field;
Structures may be initialized at declaration time by putting the list of elements in curly braces ({}):
2. Unios
A union is similar to a structure; however, it defines a single location that can be given many different field name:
the field i_value and f_value share the same space;
In a union, all fields occupy the same space, so only one may be active at a time. In other words, if you put someting in i_value, assiging something to f_value wipes out the old value of i_value.
Unions are frequently used in the area of communications. For example, suppose we have a remote tape and we want to send it four messages: open, close, read, and write.
3. typedef
C allows the programmer to define her own variable types through the typedef statement. This statement provides a way for the program to extend C's basic types. The general form of the typedef statement is:
tyepdef type-declaration;
where type-declaration is the same as a variable declaration except that a type name is used instead of a variable-name.
One frequent use of typedef's is in the definition of a new structure:
where the complex_struct always is omitted.
4. enum Type
The enumerated data type is designed for variables that contain only a limited set of values. These values are referenced by name(tag). The complier assigns each tag an integer value internally.
The general form of an enmu statement is:
enum enum-name { tag-1, tag-2, ... } variable-name;
C implements the enum types as compatible with integer.
5. Casting
this is accomplished through the cast or typecast operation. the general form of a cast is:
(type) expression
This operation tells C to compute the value of the expression, and then convert it to the specified type.
6. Bit Fields or Packed Structures
Packed structures allow us to declare structures in a way that takes up a minimum amount of storage. for example:
Packed structures should be used with care. The code to extract data from bit fields is relatively large and slow. Unless storage is a problem, packed structures should not be used.
Using packed structures for flags is clearer and less error than using bitwise operator. However, bitwise operators given the programmer additional flexibility.
7. Arrays of Structures
Structures and arrays can be combined.
Initialization of an array of structures is similar to the initialization of multi-dimensional arrays.
C provides a rich set of data types, including structures, unions, and enumerated types.
1. Structures
In a strcuture, each element or field is named and has its own data type.
The general form of a structure definition is
struct structure-name {
field-type field-name; /* comment */
field-type field-name; /* comment */
...
} variable-name;
The general form of a structure variable is
struct structure-name variable-name;
To access field in a structure, use "." operator:
variable.field;
Structures may be initialized at declaration time by putting the list of elements in curly braces ({}):
/*
* Printer cables
*/
struct bin {
char name[30]; /* name of the part */
int quantity; /* how many are in the bin */
int cost; /* The cost of a single part (in cents) */
} print_cable_bin = {
"Printer Cables", /* Name of the item in the bin */
0, /* Start with empty box */
1295 /* cost -- $12.95 */
};
2. Unios
A union is similar to a structure; however, it defines a single location that can be given many different field name:
union value {
long int i_value; /* integer version of value */
float f_fvalue; /* floating version of value */
};
the field i_value and f_value share the same space;
In a union, all fields occupy the same space, so only one may be active at a time. In other words, if you put someting in i_value, assiging something to f_value wipes out the old value of i_value.
Unions are frequently used in the area of communications. For example, suppose we have a remote tape and we want to send it four messages: open, close, read, and write.
#define DATA_MAX 1024 /* Maximum amount of data for a read and write */
struct open_msg {
char name[30]; /* Name of tape */
};
struct read_msg {
int length; /* Max data to transfer in the read */
};
struct write_msg {
int length; /* Number of bytes to write */
char data[DATA_MAX]; /* Data to write */
};
struct close_msg {
};
const int OPEN_CODE=0; /* Code indicating an open message */
const int READ_CODE=1; /* Code indicating a read message */
const int WRITE_CODE=2; /* Code indicating a write message */
const int CLOSE_CODE=3; /* Code indicating a close message */
struct msg {
int msg; /* Message type */
union {
struct open_msg open_data;
struct read_msg read_data;
struct write_msg write_data;
struct close_msg close_data;
} msg_data;
};
3. typedef
C allows the programmer to define her own variable types through the typedef statement. This statement provides a way for the program to extend C's basic types. The general form of the typedef statement is:
tyepdef type-declaration;
where type-declaration is the same as a variable declaration except that a type name is used instead of a variable-name.
One frequent use of typedef's is in the definition of a new structure:
struct complex_struct {
double real;
double imag;
};
typedef struct complex_struct complex;
the same as:
typedef struct complex_struct {
double real;
double imag;
} complex;
where the complex_struct always is omitted.
4. enum Type
The enumerated data type is designed for variables that contain only a limited set of values. These values are referenced by name(tag). The complier assigns each tag an integer value internally.
The general form of an enmu statement is:
enum enum-name { tag-1, tag-2, ... } variable-name;
C implements the enum types as compatible with integer.
5. Casting
this is accomplished through the cast or typecast operation. the general form of a cast is:
(type) expression
This operation tells C to compute the value of the expression, and then convert it to the specified type.
6. Bit Fields or Packed Structures
Packed structures allow us to declare structures in a way that takes up a minimum amount of storage. for example:
struct item {
unsigned int list:1; /* true if item is in the list */
unsigned int seen:1; /* true if this item has been seen */
unsigned int number:14; /* item number */
};
Packed structures should be used with care. The code to extract data from bit fields is relatively large and slow. Unless storage is a problem, packed structures should not be used.
Using packed structures for flags is clearer and less error than using bitwise operator. However, bitwise operators given the programmer additional flexibility.
7. Arrays of Structures
Structures and arrays can be combined.
Initialization of an array of structures is similar to the initialization of multi-dimensional arrays.