Previous: 12.1 Structures
Up: 12 Structures and Unions
Next: 12.3 Sorting Arrays of Structures
Previous Page: 12.1.4 Pointers to Structures
Next Page: 12.3 Sorting Arrays of Structures

12.2 Arrays of Structures

The inventory and the label program examples of the last section handle only a single record. More realistically, a useful program may need to handle many such records. As in previous cases where we needed to store a list of items, we can use an array as our data structure. In this case, the elements of the array are structures of a specified type. For example:

struct inventory {
     int part_no;
     float cost;
     float price;
};

struct inventory table[4];

which defines an array with four elements, each of which is of type struct inventory, i.e. each is an inventory structure.

We can think of such a data structure as a tabular representation of our data base of parts inventory with each row representing a part, and each column representing information about that part, i.e. the part_no, cost, and price, as shown in Figure 12.7.

This is very similar to a two dimensional array, except that in an array, all data items must be of the same type, where an array of structures consists of columns, each of which may be of a distinct data type. As with any array, the array name used by itself in an expression is a pointer to the entire array of structures. Therefore, the following are equivalent ways of accessing the elements of the array.

With this in mind, let us extend out address label program from Section 12.1.4 to read and print a list of labels. The code is shown in Figure 12.8 and uses the same structures and functions used in program lbl.c included in files lbl.h and lblutil.c.

In the program, the reading of labels is still performed by readlabel() only now in a while loop. The loop terminates when either MAX number of labels have been read or readlabel() returns FALSE at end of file. In this case, a new label is not read, but the value of n is incremented anyway by the ++ operator. Thus, if the loop is terminated because of an end of file, the incremented value of n must be decremented to correctly indicate the number of entries in the array. Finally, labels are printed using printlabel() in a loop.

Sample Session:

Next, let us revise the payroll program so that a payroll data record is stored in a structure called payrecord. Let us also define a type called payrecord for the structure data type that houses a payroll data record:

typedef struct payrecord payrecord;
We may, thus, declare variables of type payrecord rather than struct payrecord. The name for the structure tag and the defined data type can be the same as shown. The structure definitions and typedef are placed in the file payrec.h and shown in Figure 12.9.

The program logic is simple enough - it reads input data, calculates payroll data, and prints payroll data as before. In this implementation, we will also include calculation of tax withheld. The result is that we have gross pay, net pay, and tax withheld as additional items in payroll data records as seen in the structure definitions. The program also keeps track of totals for gross and net pay disbursed as well as for taxes withheld. The totals are printed as a summary statement for the payroll. Figure 12.10 shows the main driver.

The function readrecords() reads the input data records into an array and returns the number of records read, printrecords() prints all payroll data records, and printsummary() prints the totals of gross pay and taxes withheld. Finally, we need calcrecords() to calculate pay for each of the records. These functions are shown in Figures 12.11 and 12.12.

In the code, we use functions readname() and printname() to read and print an individual name for each record. Finally, we must write calcrecords() which calculates the pay for each data record and the totals of gross pay and tax withheld. The tax is calculated on the following basis:

If the total pay is $500 or less, the tax is 15%;
If the total is $1000 or less, the tax is 28%;
If the total is over $1000, the tax is 33%.

The function also keeps a cumulative sum of total gross pay and total tax withheld. Finally, it returns total gross pay and indirectly returns the total tax withheld. The code is shown in Figure 12.13. Here is a sample interaction with the program:



Previous: 12.1 Structures
Up: 12 Structures and Unions
Next: 12.3 Sorting Arrays of Structures
Previous Page: 12.1.4 Pointers to Structures
Next Page: 12.3 Sorting Arrays of Structures

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:12:43 HST 1994