I l@ve RuBoard Previous Section Next Section

12.1 Structures

Suppose you are writing an inventory program for a warehouse. The warehouse is filled with bins, each containing a bunch of parts. All the parts in a bin are identical, so you don't have to worry about mixed bins or partials.

For each bin you need to know:

  • The name of the part it holds (30-character string).

  • The quantity on hand (integer).

  • The price (integer cents).

In previous chapters you have used arrays for storing a group of similar data types, but in this example you have a mixed bag: two integers and a string.

Instead of an array, you will use a new data type called a structure. In an array, all the elements are of the same type and are numbered. In a structure, each element, or member, is named and has its own data type.

The general form of a structure definition is:

struct structure-name { 
    member-type member-name;  // Comment 
    member-type member-name;  // Comment 
    . . . . 
} variable-name; 

For example, say you want to define a bin to hold printer cables. The structure definition is:

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)
} printer_cable_box;    // Where we put the print cables

This definition actually tells C++ two things. The first is what a struct bin looks like. This statement defines a new data type that can be used in declaring other variables. This statement also declares the variable printer_cable_box. Since the structure of a bin has been defined, you can use it to declare additional variables:

struct bin terminal_cable_box;  // Place to put terminal cables

The structure-name part of the definition may be omitted:

struct { 
    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

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:

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)
}; 

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:

struct bin printer_cable_box; // Define the box holding printer cables

C++ allows the struct to be omitted, so you can use the following declaration:

bin printer_cable_box; // Define the box holding printer cables

You have defined the variable printer_cable_box containing three named members: name, quantity, and cost. To access them you use the syntax:

variable.member

For example, if you just found out that the price of the cables went up to $12.95, you would do the following:

printer_cable_box.cost = 1295;   // $12.95 is the new price

To compute the value of everything in the bin, you can simply multiply the cost by the number of items using the following:

total_cost = printer_cable_box.cost * printer_cable_box.quantity; 

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:

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)
} printer_cable_box = { 
    "Printer Cables",   // Name of the item in the bin
    0,                  // Start with empty box
    1295                // Cost -- $12.95
}; 
    I l@ve RuBoard Previous Section Next Section