Structures
- In an array, all the elements are of the same type and are numbered.
- In a structure, each element, or field, is named and has its own data type.
- The general form of a structure definition is:
field-type field-name // Comment
....
} variable-name;
- For example,
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)
} printer_cable_box; // Where we put the print cables
- Statement defines a new data type that can be used in declaring other variables.
- The structure-name part of the definition may be omitted.
char name[30]; // Name of the part
int quantity; // How many are in the bin
int cost;// of a single part (in cents)
} printer_cable_box; // Where we put the print cables
- The variable printer_cable_box is still to be defined, but no data type is created. The data type for this variable is an anonymous structure.
- The variable-name part also may be omitted. This would define a structure type but no variables.
- In an extreme case, both the variable-name and the structure-name parts may be omitted. This creates a section of correct but totally useless code.
- Once the structure type has been defined you can use it to define variables:
- C++ allows the struct to be omitted, so you can use the following declaration:
- To access structure fields use the syntax:
- Example,
- 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)
};
struct bin printer_cable_box = {
"Printer Cables", // Name of the item in the bin
0, // Start with empty box
1295 // Cost -- $12.95
};
- The definition of the structure bin and the variable printer_cable_box can be combined in one step:
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)
} printer_cable_box = {
"Printer Cables", // Name of the item in the bin
0, // Start with empty box
1295 // Cost -- $12.95
};
Bit Fields or Packed Structures
- Packed structures allow you to declare structures in a way that takes up a minimum of storage.
- Syntax:
- Structure field: number of bits to be used for that field.
- Storage saving in using packaged structure: For example, the following structure takes up 6 bytes (on a 16-bit machine):
unsigned int list; // True if item is in the list
unsigned int seen; // True if this item has been seen
unsigned int number; // Item number
};
- Using packaged structure to save space: If the fields list and seen can have only two values, 0 and 1, so only 1 bit is needed to represent them. You never plan on having more than 16383 items (0x3fff or 14 bits). You can redefine this structure using bit fields, so, it takes only 2 bytes, by following each field with a colon and the
unsigned int list:l; // True if item is in the list
unsigned int seen:l; // True if this item has been seen
unsigned int number:14; // Item number
};
- Packed structures should be used with care. The machine code to extract data from bit fields is relatively large and slow. Unless storage is a problem, packed structures should not be used.
- Bit Operations, you needed to store character data and five
status flags for 8,000 characters. In this case, using a different
byte for each flag would eat up a lot of storage (five bytes for
each incoming character). You used bitwise operations to pack the
five flags into a single byte. Alternatively, a packed structure
could have accomplished the same thing:
char character; // Character from device
int error:1; // True if any error is set
int framing_error:l;// A framing error occurred
int parity_error:l; // Character had the wrong parity
int carrier_lost:l; // The carrier signal went down
int channel_down:1; // Power was lost on the channel
};
- Using packed structures for flags is clearer and less error-prone than using bitwise operators.
- However, bitwise operators allow additional flexibility. You should use the one that is clearest and easiest for you to use.
Arrays of Structures
- Structures and arrays can be combined.
- Example 1
struct time {
int hour; // Hour (24-hour clock)
int minute; // 0-59
int second; // 0-59
};
#define MAX_LAPS 4 /* We will have only 4 laps*/
/* The time of day for each lap*/
struct time lap[MAX_LAPS]; // defines lap as an array of four elements. Each element consists of a single time structure
- Usage
/*
* Runner just past the timing point
*/
lap[count].hour = hour;
lap[count].minute = minute;
lap[count].second = second;
++count;
- Example 2
struct mailing {
char name[60]; // Last name, first name
char addressl[60]; // Two lines of street address
char address2[60];
char city[40]; // Name of the city
char state[2]; // Two-character abbreviation
long int zip; // Numeric zip code
};
You can now declare an array to hold the mailing list:
/* Our mailing list */
struct mailing list[MAX_ENTRIES];
- Initialization
* Initialization of an array of structures is similar to the initialization of multidimensional arrays.
struct time start_stop[2] = {
{10, 0, 0},
{12, 0, 0}
};
In the next lesson, we learn about typedefs and enums. Till then, keep practicing!